说我有一个像这样的对象......
我有一个像这样的ID的数组..
array = ['21d32fwef23fw32f3', '21we3weasdf23rfwfwf3']
如何仅返回与sys.ids
ids
匹配array
的对象
我试过像这样使用lodash ..
getWeekItems(weekNumber) {
this.contenfulService.getWeekItems(weekNumber)
.then((weekOverview) => {
this.weekOverviewCompleted = _.filter(weekOverview, this.completedIds);
console.log(this.weekOverviewCompleted);
}).then(() => console.log(this.weekOverview));
}
但我得到一个空数组?
有更好的方法吗?
答案 0 :(得分:1)
这是您获取过滤结果的方法:
_.filter(weekOverview, (item) => {
return this.completedIds.indexOf(item.sys.id) > -1 ;
});
// OR Simple javascript filter function
weekOverview.filter(item => {
return this.completedIds.indexOf(item.sys.id) > -1 ;
})