[Vuejs]-Cannot Array.filter properly

2👍

based on MDN:

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

so basically what is wrong in your code is that:

state.filters.geologies.filter(elem => !elem.includes(payload))

is not being saved in any variable, thus the filtered array isn’t being used. in order to make it work you need to assign the return value of the filter. something like:

state.filters.geologies = state.filters.geologies.filter(elem => !elem.includes(payload))

or as vladislav said:

state.filters.geologies = [...state.filters.geologies.filter(elem => !elem.includes(payload))]

2👍

Until splice() method is mutating original array, filter(), map(), or some() methods returns new array and leave the original array intact. So, you can use filter() method, but then you should replace original array with returned array. For example:

UPDATE_FILTERS_GEOLOGIES: (state, payload) => {
  let g = state.filter.geologies
  let p = payload

  g.some(e => e === p)
    ? (g = { ...g.filter(e => !e.includes(p)) })
    : g.push(p)
}

Leave a comment