To remove duplicate form an array with JavaScript, we can create a set.
For instance, we write
const array = [
{ id: 123, value: "value1", name: "Name1" },
{ id: 124, value: "value2", name: "Name1" },
{ id: 125, value: "value3", name: "Name2" },
{ id: 126, value: "value4", name: "Name2" },
];
const names = [...new Set(array.map((a) => a.name))];
console.log(names);
to call map
to return an array of name
property values from the objects in array
.
Then we convert it to a set with the Set
constructor.
Then we use the spread operator to convert the set into an array.