lodash通过数组Angular 5过滤对象数组

时间:2018-03-28 04:30:32

标签: javascript arrays angular typescript lodash

说我有一个像这样的对象......

enter image description here

我有一个像这样的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));
  }

但我得到一个空数组?

有更好的方法吗?

1 个答案:

答案 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 ;    
})