i have following arrays:
arr1 = [{ id: 1, name: 'diego', age: 23 }, { id: 2, name: 'brian', age: 18 }] arr2 = [{ id: 1, name: 'diego', age: 23 }, { id: 2, name: 'brian', age: 18 }, { id: 3, name: 'pikachu', age: 88 }]
i need difference between 2 arrays, espected result is:
arr3 [{id:3, name: 'pikachu', age: 88}]
how solve problem using es6
or typescript
?
i tried using set, doesn't worked.
something maybe:
let ids1 = arr1.map(item => item.id); let ids2 = arr2.map(item => item.id); let diff = ids1.map((id, index) => { if (ids2.indexof(id) < 0) { return arr1[index]; } }).concat(ids2.map((id, index) => { if (ids1.indexof(id) < 0) { return arr2[index]; } })).filter(item => item != undefined);
Comments
Post a Comment