Filter array of object from another array

后端 未结 1 1822
庸人自扰
庸人自扰 2021-01-24 07:43

I have two arrays. I want to filter one array which contains objects from another array.

let array1= [{date:1, count:4}, {date:3, count:6}];
let array2= [1,2,3,4         


        
1条回答
  •  心在旅途
    2021-01-24 08:31

    You can use map() and find() methods for this. You don't need filter() because for each element you will return count or 0 so you can just use map().

    let array1= [{date:1, count:4}, {date:3, count:6}];
    let array2= [1,2,3,4];
    
    var array = array2.map(function(e) {
      var f = array1.find(a => a.date == e);
      return f ? f.count : 0
    });
    
    console.log(array)

    0 讨论(0)
提交回复
热议问题