过滤器在 Vuex 中过滤对象数组的操作中不起作用 [英] Filter is not working inside the action for filtering array of objects in Vuex
本文介绍了过滤器在 Vuex 中过滤对象数组的操作中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个 getter 对象数组.我在 action 内使用 getter 并尝试对其进行过滤,但无论我做什么过滤都不起作用,它会返回所有映射的项目 ID.
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)
},
怎么了?
推荐答案
我认为问题出在这里:
item.type !== 'hat' || item.type !== 'glases'
- 这总是返回
true
因为item.type
永远不会等同于'hat'
和'glases'
同时. - 我怀疑
'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
这篇关于过滤器在 Vuex 中过滤对象数组的操作中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文