Open In App

How to remove duplicates from an array of objects using JavaScript ?

In this article, we are given an array of objects and the task is to remove the duplicate object element from the array list.



These are the methods to solve this problem which are discussed below:

Method 1: Using one of the keys as an index

Example: This example is the implementation of the above-explained method.




function removeDuplicates() {
 
    // Create an array of objects
    books = [
        { title: "C++", author: "Bjarne" },
        { title: "Java", author: "James" },
        { title: "Python", author: "Guido" },
        { title: "Java", author: "James" },
    ];
 
    // Declare a new array
    let newArray = [];
 
    // Declare an empty object
    let uniqueObject = {};
 
    // Loop for the array elements
    for (let i in books) {
 
        // Extract the title
        objTitle = books[i]['title'];
 
        // Use the title as the index
        uniqueObject[objTitle] = books[i];
    }
 
    // Loop to push unique object into array
    for (i in uniqueObject) {
        newArray.push(uniqueObject[i]);
    }
 
    // Display the unique objects
    console.log(newArray);
}
removeDuplicates();

Output
[
  { title: 'C++', author: 'Bjarne' },
  { title: 'Java', author: 'James' },
  { title: 'Python', author: 'Guido' }
]

Method 2: Converting the array to a Set to remove the duplicates

Example: In this example, we will remove the duplicate values from an array of objects by converting the array to a set to remove the duplicates.




function removeDuplicates() {
 
    // Create an array of objects
    books = [
        { title: "C++", author: "Bjarne" },
        { title: "Java", author: "James" },
        { title: "Python", author: "Guido" },
        { title: "Java", author: "James" },
    ];
 
    jsonObject = books.map(JSON.stringify);
    uniqueSet = new Set(jsonObject);
    uniqueArray = Array.from(uniqueSet).map(JSON.parse);
 
    console.log(uniqueArray);
}
removeDuplicates();

Output
[
  { title: 'C++', author: 'Bjarne' },
  { title: 'Java', author: 'James' },
  { title: 'Python', author: 'Guido' }
]

Method 3: Using filter() and includes() Method

In this method, we will filter the data from the book and include the data which is not repeated in a new array.

Example: This example is the implementation of the above-explained method.




const books = [
    { title: "C++", author: "Bjarne" },
    { title: "Java", author: "James" },
    { title: "Python", author: "Guido" },
    { title: "Java", author: "James" },
];
 
const ids = books.map(({ title }) => title);
const filtered = books.filter(({ title }, index) =>
    !ids.includes(title, index + 1));
 
console.log(filtered);

Output
[
  { title: 'C++', author: 'Bjarne' },
  { title: 'Python', author: 'Guido' },
  { title: 'Java', author: 'James' }
]

Method 4: Using filter() and findIndex() Method

In this method, we will filter() the array by using the Array.findIndex() method to return the first index of the element in a given array that satisfies the provided testing function (passed in by the user while calling)

Example: This example is the implementation of the above-explained method.




const books = [
    { title: "C++", author: "Bjarne" },
    { title: "Java", author: "James" },
    { title: "Python", author: "Guido" },
    { title: "Java", author: "James" },
];
 
const unique = books.filter((obj, index) => {
    return index === books.findIndex(o => obj.title === o.title);
});
 
console.log(unique);

Output
[
  { title: 'C++', author: 'Bjarne' },
  { title: 'Java', author: 'James' },
  { title: 'Python', author: 'Guido' }
]

Method 5: Using Lodash _.uniq() method

In this method, we are using _.uniq() method which returns the unique value present in the given array.

Example: This example is the implementation of the above-explained method.




// Requiring the lodash library
const _ = require("lodash");
 
// Use of _.uniq()
// method
let arr = _.uniq([{ 'x': 1 }, { 'x': 2 }, { 'x': 1 }], 'x');
 
// Printing the output
console.log(arr);

Output:

 [{ 'x': 1 }, { 'x': 2 }]

Article Tags :