英文:
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]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论