JSON Remove Duplicate Objects

JSON Remove Duplicate Objects (by name property)

In the following example, we will remove an object if the name property of an object matches the other.

Example

In this example, the third object will be removed as the name ('Danny') matches the name of the first object.

HTML Editor
<!DOCTYPE html> <html lang="en-US"> <body> <h1>JSON Remove Duplicate Objects</h1> <p>Remove duplicate objects by name.</p> <button onclick="myFunction()">Click Me</button> <script> var data = [{ "name": "Danny", "age": 25 }, { "name": "Mike", "age": 25 }, { "name": "Danny", "age": 18 } ] var data1 = []; function removeDup(obj, index) { if (data1.map(function(e) { return e.name; }).indexOf(obj.name) == -1) { data1.push(obj); } if (index == 2) console.log(data1); } function myFunction() { data.filter(removeDup, 0) } </script> </body> </html>

JSON Remove Duplicate Objects

In the following example, we will remove an object only if all the properties of an object matches the other.

Example

In this example, no objects will be removed as all the properties of all the objects are unique.

HTML Editor
<!DOCTYPE html> <html lang="en-US"> <body> <h1>JSON Remove Duplicate Objects</h1> <p>Remove duplicate objects.</p> <button onclick="myFunction()">Click Me</button> <script> var data = [{ "name": "Danny", "age": 25 }, { "name": "Mike", "age": 21 }, { "name": "Danny", "age": 17 } ] var data1 = data.filter((thing, index) => { return index === data.findIndex(obj => { return JSON.stringify(obj) === JSON.stringify(thing); }); }); function myFunction() { console.log(data1); } </script> </body> </html>

Join Our Channel

Join our telegram channel to get an instant update on depreciation and new features on HTML, CSS, JavaScript, jQuery, Node.js, PHP and Python.

This channel is primarily useful for Full Stack Web Developer.

Share this Page

Meet the Author