我有一个像这样的对象数组
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
的情况下返回事件答案 0 :(得分:2)
Array.prototype.some
返回true
如果所有数组元素都匹配条件<{p>,则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);
}
答案 1 :(得分:0)
看起来您希望对多个字段进行过滤,因此最好使用高阶函数创建过滤函数,可以找到详细示例here。
我认为你实际上是在寻找一些而不是每一次的反转。
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)
);
&#13;