Vue在getter中链式多个过滤器。

huangapple go评论71阅读模式
英文:

Vue chain multiple filters in getter

问题

在Getter中,我需要筛选一个数组并返回符合两个条件的值。
我需要返回那些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]
}

这两个过滤器的工作正常。如何将它们链接在一起以创建一个新数组?

英文:

On a Getter I need to filter an array and return values that match 2 conditions.
I need to return item_categories that don't have an item_category_location equal to current location or don't have item_category_locations at all.

 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
    
            },

The 2 filters work fine. How can I chain them to create a new array?

答案1

得分: 2

你可以这样做:

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

英文:

You can do it like so:

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

huangapple
  • 本文由 发表于 2020年1月6日 17:21:42
  • 转载请务必保留本文链接:https://go.coder-hub.com/59609475.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定