How to merge two array of objects and make it unique based on a property?
We can merge two arrays of object in many ways. Following is an way to do it using Map:
const arr1 = [ {name: 'Trump', isPresident: true}, {name: 'Cruz', isPresident: true}, {name: 'Kasich', isPresident: true}, {name: 'Joe', isPresident: true} ]; const arr2 = [ {name: 'Cruz', isPresident: false}, {name: 'Kasich', isPresident: false}, {name: 'Trump', isPresident: true, party: 'Republican'} ]; const result = [...new Map([...arr1, ...arr2] .map((item) => [item.name, item])).values()]; console.log(result);