4

How To Remove Duplicates From a JavaScript Object

 3 years ago
source link: https://hackernoon.com/how-to-remove-duplicates-from-a-javascript-object-401r3462
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

@ashykAshok Kumar

Javascript developer

We can use the Set object to remove the duplicates from an array. The Set object lets you store unique values of any type, whether primitive values or object references. This property can be used to store only the objects that are unique in the array.

0 reactions

Here's an example:

0 reactions
const books = [
          { title: "C++", author: "Bjarne" },
          { title: "Java", author: "James" },
          { title: "C++", author: "Bjarne" },
          { title: "C++", author: "Bjarne" },
];              
const jsonObj = books.map(JSON.stringify);
const set = new Set(jsonObj);
const uniqueArray = Array.from(set).map(JSON.parse);
console.log(uniqueArray);
/*
[
   { title: "C++", author: "Bjarne" },
   { title: "Java", author: "James" },
]*/

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK