Reputation: 785
I have a function where I have to return an array of elements with matching filters. Here's the function code:
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
}
}
I would like to filter the masterFilterList
to return an array of items
filterNameToSearch = 'Item2'
under the following conditions:
1) for each array element (assume appliedFilterItem
) in appliedFilters
, compare appliedFilterItem.name
with masterFilterItem[appliedFilterItem.name]
and check if any of the filters in appliedFilterItem
has the same value as masterFilterItem[appliedFilterItem.name]
2) The condition has to look like something like 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
. But the number of elements in the appliedFilters
is dynamic.
So please help me out with this issue?
Upvotes: 0
Views: 66
Reputation: 138297
return masterFilterList.map(el => el[filterNameToSearch])
.filter(value =>
appliedFilters.some(({name, filters}) =>
name === filterNameToSearch &&
filters.some(filter =>
filter.status === "selected" &&
filter.value === value
)
)
);
Upvotes: 2