[英]filter an array of objects with another array of object with filter and some
我有一個這樣的對象數組
events = [ {
'summary': 'sample test events1',
'location': 'coimbatore',
'start': {
'date': '2018-08-28',
'timeZone': 'America/Los_Angeles'
},
'end': {
'date': '2018-08-28',
'timeZone': 'America/Los_Angeles'
}
},
{
'summary': 'sample test events2',
'location': 'coimbatore',
'start': {
'date': '2018-08-29',
'timeZone': 'America/Los_Angeles'
},
'end': {
'date': '2018-08-29',
'timeZone': 'America/Los_Angeles'
}
},
{
'summary': 'sample test events4',
'location': 'coimbatore',
'start': {
'date': '2018-08-27',
'timeZone': 'America/Los_Angeles'
},
'end': {
'date': '2018-08-27',
'timeZone': 'America/Los_Angeles'
}
},
{
'summary': 'sample test events5',
'location': 'coimbatore',
'start': {
'date': '2018-08-26',
'timeZone': 'America/Los_Angeles'
},
'end': {
'date': '2018-08-26',
'timeZone': 'America/Los_Angeles'
}
}];
我還有另一個要過濾的對象數組
toFilterEvents = [
{startDate: "2018-08-28", summary: "sample test events1"},
{startDate: "2018-08-29", summary: "sample test events2"},
]
我希望結果像
events = [
{
'summary': 'sample test events4',
'location': 'coimbatore',
'start': {
'date': '2018-08-27',
'timeZone': 'America/Los_Angeles'
},
'end': {
'date': '2018-08-27',
'timeZone': 'America/Los_Angeles'
}
},
{
'summary': 'sample test events5',
'location': 'coimbatore',
'start': {
'date': '2018-08-26',
'timeZone': 'America/Los_Angeles'
},
'end': {
'date': '2018-08-26',
'timeZone': 'America/Los_Angeles'
}
}];
我嘗試過的
filterExistingEvents(toFilterEvents);
filterExistingEvents(filtered_events) {
const hello = this.events.filter((r, i) => {
return filtered_events.some(f => r.summary !== f.summary)
});
console.log('events after filter', hello, this.events);
}
如您所見,我正在使用過濾器和一些過濾器來獲得所需的輸出,如上所示,但它不起作用。 我發現了類似的問題,但這是返回沒有toFilterEvents的事件
Array.prototype.some
返回true
,如果數組元素的ATLEAST一個相匹配的條件Array.prototype.every
返回true
,如果所有的數組元素的條件匹配
filterExistingEvents(filtered_events) {
const hello = this.events.filter((r, i) => {
return filtered_events.every(f => r.summary !== f.summary)
});
console.log('events after filter', hello, this.events);
}
看起來您想對多個字段進行過濾,因此最好從高階函數中創建過濾器函數,有關詳細示例,請參見此處 。
我認為您實際上是在尋找某些而非全部的反轉。
const events = [{ 'summary': 'sample test events1', 'location': 'coimbatore', 'start': { 'date': '2018-08-28', 'timeZone': 'America/Los_Angeles' }, 'end': { 'date': '2018-08-28', 'timeZone': 'America/Los_Angeles' } }, { 'summary': 'sample test events2', 'location': 'coimbatore', 'start': { 'date': '2018-08-29', 'timeZone': 'America/Los_Angeles' }, 'end': { 'date': '2018-08-29', 'timeZone': 'America/Los_Angeles' } }, { 'summary': 'sample test events4', 'location': 'coimbatore', 'start': { 'date': '2018-08-27', 'timeZone': 'America/Los_Angeles' }, 'end': { 'date': '2018-08-27', 'timeZone': 'America/Los_Angeles' } }, { 'summary': 'sample test events5', 'location': 'coimbatore', 'start': { 'date': '2018-08-26', 'timeZone': 'America/Los_Angeles' }, 'end': { 'date': '2018-08-26', 'timeZone': 'America/Los_Angeles' } }]; const filterFn = getter => comparer => o => comparer(getter(o)) ; const isNotIn = hayStack => needle => !hayStack.some(x=>x===needle) ; const summary = o => o.summary; const startDate = o => o.start.date; const toFilterEvents = [ { startDate: "2018-08-28", summary: "sample test events1" }, { startDate: "2018-08-29", summary: "sample test events2" }, ]; //filter out any events that have summary or startDate in toFilterEvents console.log( events.filter( filterFn(summary)(isNotIn(toFilterEvents.map(x=>x.summary))) ).filter( filterFn(startDate)(isNotIn(toFilterEvents.map(x=>x.startDate))) ).map(x=>x.summary) ); //filter out any events that have summary and startDate in toFilterEvents console.log( events.filter( ( (summaries,startDates)=>item=> filterFn(summary)(isNotIn(summaries))(item) || filterFn(startDate)(isNotIn(startDates))(item) )(toFilterEvents.map(x=>x.summary),toFilterEvents.map(x=>x.startDate)) ).map(x=>x.summary) );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.