我想通过将选项数组传递到也是数组的字段中来过滤列表
let myFilters = ['filter1', 'filter4'];
let myArray = [
{ name: 'item1', options: ['filter1', 'filter5'] },
{ name: 'item2', options: ['filter1', 'filter2'] }
];
let myResultFiltered = myArray.filter(item =>
myFilters.includes(item.options))
答案 0 :(得分:0)
类似的事情应该起作用:
let myFilters = ['filter1', 'filter4'];
let myArray = [
{ name: 'item1', options: ['filter1', 'filter5'] }, // OUTPUT FOR: OR
{ name: 'item2', options: ['filter1', 'filter2'] }, // OUTPUT FOR: OR
{ name: 'item3', options: ['filter1', 'filter4'] }, // OUTPUT FOR: OR, AND
{ name: 'item4', options: ['filter2', 'filter3'] } // OUTPUT FOR: NONE
];
// Includes each entry that fulfills AT LEAST ONE of the filter options
let myResultFilteredOR = myArray
.filter(item => myFilters
.some(myFilter=> item.options // any ONE filter
.some(option => option === myFilter) // is AT LEAST ONCE in options
)
);
// Includes each entry that fulfills ALL of the filter options
let myResultFilteredAND = myArray
.filter(item => myFilters
.every(myFilter=> item.options // EVERY filter
.some(option => option === myFilter) // is AT LEAST ONCE in Options
)
);