I've got an array of objects. All I want is to check if the ids of the objects are the same and return that particular object, without duplication.
example.
let arr1 = [
{id: 1, name: 'A'},
{id: 3, name: 'C'},
{id: 1, name: 'A'},
{id: 2, name: 'B'},
{id: 2, name: 'B'}
]
result I want:
let newArr = [
{id: 1, name: 'A'},
{id: 2, name: 'B'}
]
result I get:
let arr1 = [
{id: 1, name: 'A'},
{id: 3, name: 'C'},
{id: 2, name: 'B'}
]
I tried:
arr1.reduce((destArray, obj) => {
if (destArray.findIndex(i => i.id === obj.id) < 0) {
return destArray.concat(obj);
} else {
return destArray;
}
}, [])
As a result, I get objects with matching ids, BUT there're also objects with kinda unique id. But I don't need them.