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
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)