我有一个函数,必须返回具有匹配过滤器的元素数组。这是功能代码:
filter_getCustomFilterItems(filterNameToSearch: string, appliedFilters: Array<any>) {
let tempFilterArray = [];
let masterFilterList = getMasterFilterList();
/*
filterNameToSearch can be a string either as `Item2` or `Item3`
masterFilterList will be of type
masterFilterList: Array<any> = [
{
Item1: 'some-item-1',
Item2: 'some-item-2',
Item3: 'some-item-3',
Item4: 'some-item-5',
},
{
Item1: 'some-item-10',
Item2: 'some-item-11',
Item3: 'some-item-12',
Item4: 'some-item-13',
},......
];
appliedFilters will be of type
appliedFilters: Array<any> = [
{
name: 'Item1',
filters: [
{value: 'some-item-1', status: 'selected'},
{value: 'some-item-10', status: 'selected'}
]
},......
];
*/
for (let masterFilterItem of masterFilterList) {
//Missing logic here
}
}
我想过滤
masterFilterList
以返回项目数组filterNameToSearch = 'Item2'
在以下条件下:1)对于
appliedFilterItem
中的每个数组元素(假定appliedFilters
),将appliedFilterItem.name
与masterFilterItem[appliedFilterItem.name]
进行比较,并检查appliedFilterItem
中的任何过滤器是否具有与masterFilterItem[appliedFilterItem.name]
相同的值2)条件必须看起来像
masterFilterItem[appliedFilterItem[0].name] == appliedFilterItem[0].filters[0].value && appliedFilterItem[0].filters[0].status === 'selected' && masterFilterItem[appliedFilterItem[1].name] == appliedFilterItem[1].filters[0].value && appliedFilterItem[1].filters[0].status === 'selected' and so on for all appliedFilterItems
。但是appliedFilters
中的元素数量是动态的。所以请帮我解决这个问题?
最佳答案
return masterFilterList.map(el => el[filterNameToSearch])
.filter(value =>
appliedFilters.some(({name, filters}) =>
name === filterNameToSearch &&
filters.some(filter =>
filter.status === "selected" &&
filter.value === value
)
)
);