【问题标题】:Vue chain multiple filters in getterVue在getter中链接多个过滤器
【发布时间】:2020-01-06 09:21:42
【问题描述】:

在 Getter 上,我需要过滤一个数组并返回符合 2 个条件的值。 我需要返回 item_categories 的 item_category_location 不等于当前位置或根本没有 item_category_locations。

 unaddedItemCategories(state, getters, rootState) {
                let otherLocations = state.item_categories
                    .filter((item_category) =>
                        item_category.item_category_locations.some((item_category_location) => item_category_location.location_id !== rootState.currentLocation.id))
                let noLocations = state.item_categories
                    .filter(item_category => item_category.item_category_locations.length == 0)
                return otherLocations, noLocations

            },

2 个过滤器工作正常。如何链接它们以创建新数组?

【问题讨论】:

  • 为什么在退货声明中使用comma
  • return 语句其实没什么意义,只是显示我想返回什么
  • 如果您只是使用单个 filter 和逻辑 OR || 而不是过滤两次,这不是更清楚吗?如果也是迭代次数的一半。
  • 是的,但我无法正确添加 || 的语法在其他位置。但肯定会更好。如果您可以在答案中显示它,我会接受

标签: vue.js vuex


【解决方案1】:

你可以这样做:

return [...otherLocations, ...noLocations]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-04
    • 1970-01-01
    • 2020-03-18
    • 2020-07-27
    • 1970-01-01
    相关资源
    最近更新 更多