To get list of duplicate objects in an array of objects with JavaScript, we use some array methods.
For instance, we write
const srr = [
{ id: 10, name: "someName1" },
{ id: 10, name: "someName2" },
{ id: 11, name: "someName3" },
{ id: 12, name: "someName4" },
];
const unique = arr
.map((e) => e.id)
.map((e, i, final) => final.indexOf(e) === i && i)
.filter((obj) => arr[obj])
.map((e) => arr[e]);
to call arr.map
with a callback to return an array of id
property values.
Then we call map
with a callback to get the index of the id
value in.
And then we call filter
to get the first instance of the object with the given id
in arr
with filter
.