过滤复杂对象中的数组数组和 return 该对象

Filter array of array within a complex object and return that object

我试过这样写管道:

这就是我需要达到的目标

the filter will allow any objects(items/ItemModel[]) that don't include selected colors (res/FilterMenuDataModel) to be hidden

selected Colors mean's colorIds array.

 transform(items: ItemModel[]): Observable<ItemModel[]> {
    return this.menuElementsDataService.filterMenuFinalDataChanged$.pipe(
      map((res) => {
        if (!res) {
          return items;
        }

        return filter(items, (i) => 'need your help here';
      })
    );
  }

item.model.ts

export interface ItemModel {
  itemId?: number;
  itemName?: string;
  colorIds?: number[]; // one array is this
}

过滤菜单数据-model.ts

export interface FilterMenuDataModel {
 
  colorIds?: number[]; // the other array is this
}

注意: res 对象有 FilterMenuDataModel 个属性。

我已经尝试了很多 things/many 小时,但仍然没有成功。你知道怎么做吗?我在这里使用了Lodash。但是 Javascript 方式也很好,因此我可以稍后将其转换为 Lodash。

我想我明白目标是找到与 filterMenu 选择的项目没有共同 colorId 的项目。 Lodash提供intersection(),所以过滤谓词可以是一个空交集的测试,如下...

const items = [
  { itemId: 0, itemName: 'zero', colorIds: [32, 33, 34] },
  { itemId: 1, itemName: 'one', colorIds: [33, 34, 35] },
  { itemId: 2, itemName: 'two', colorIds: [34, 35, 36] },
];

// only item one above does not include colors 32 and 36
const filterMenuData = { colorIds: [32, 36] };

const hideTheseItems = items.filter(item => {
  return _.intersection(item.colorIds, filterMenuData.colorIds).length === 0;
});
console.log(hideTheseItems);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/lodash.min.js"></script>