Filter is not working inside the action for filtering array of objects in Vuex

淺唱寂寞╮ 提交于 2021-01-07 06:31:29

问题


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'
  1. This always returns true because item.type will never be equivalent to 'hat' and 'glases' at the same time.
  2. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!