我有一个函数,必须返回具有匹配过滤器的元素数组。这是功能代码:

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.namemasterFilterItem[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
            )
         )
      );

09-25 18:49