问题
I have an array of objects which is getter. I am using getter inside action and trying to filter it but whatever I do filtering doesn't work and it returns all mapped item ids.
filterItems({ getters, commit }) {
let filteredItems = getters.getAllItems
.filter(item => item.type !== 'hat' || item.type !== 'glases')
.map(item => item.id)
console.log(filterItems)
commit('setFilteredItems', filteredItems)
},
What is wrong?
回答1:
I think the problem is coming from here:
item.type !== 'hat' || item.type !== 'glases'
- This always returns
true
becauseitem.type
will never be equivalent to'hat'
and'glases'
at the same time. - I doubt that
'glases'
is a correct spelling? This might be a problem or just a mis-typing
来源:https://stackoverflow.com/questions/65236124/filter-is-not-working-inside-the-action-for-filtering-array-of-objects-in-vuex