我試過像這樣寫管道:
這是我需要實作的
過濾器將允許隱藏任何
items/ItemModel[]不包含所選顏色 (res/FilterMenuDataModel) 的物件 ( )
選定的顏色均值
colorIds陣列。
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
}
filter-menu-data-model.ts
export interface FilterMenuDataModel {
colorIds?: number[]; // the other array is this
}
注意: res物件具有FilterMenuDataModel屬性。
我已經嘗試了很多事情/很多小時但沒有任何運氣。你知道怎么做嗎?我在這里使用了Lodash。但是 Javascript 方式也很好,因此我可以稍后將其轉換為 Lodash。
uj5u.com熱心網友回復:
我想我明白目的是找到與 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>
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/338719.html
標籤:javascript 有角的 打字稿 离子框架 洛达什
