How to use filter() and indexOf() methods In Javascript

The approach uses filter() and indexOf() to create a unique array by checking the index of each object’s first occurrence based on JSON string representation for precise comparison. This ensures the removal of duplicate objects from the original array.

Syntax:

let arr = inputArray.filter((value, index, self) => {
  return self.indexOf(value) === index;
});

Example: Filtering out duplicate objects from the input array using filter() and indexOf() based on JSON string comparison, resulting in a unique array.

Javascript
let arr = [
{ id: 1, name: "Geek1" },
{ id: 2, name: "Geek2" },
{ id: 1, name: "Geek1" },
{ id: 3, name: "Geek3" },
];

// Unique array using filter and indexOf functions
let uniqueArr = arr.filter(
(value, index, self) =>
    self.findIndex((obj) => 
        JSON.stringify(obj) === JSON.stringify(value)) ===
    index
);

// Printing output
console.log("Input Array:", arr);
console.log("Unique Array:", uniqueArr);

Output
Input Array: [
  { id: 1, name: 'Geek1' },
  { id: 2, name: 'Geek2' },
  { id: 1, name: 'Geek1' },
  { id: 3, name: 'Geek3' }
]
Unique Array: [
  { id: 1, name: 'Geek1' },
  { id: 2, name: 'Geek2' },
...

How to Convert Array of Objects into Unique Array of Objects in JavaScript ?

Arrays of objects are a common data structure in JavaScript, often used to store and manipulate collections of related data. However, there are scenarios where you may need to convert an array of objects into a unique array, removing any duplicate objects based on specific criteria. JavaScript has various methods to convert an array of objects into a unique array of objects which are as follows:

Table of Content

  • Using Set
  • Using filter() and indexOf() methods
  • Using Map
  • Using reduce()
  • Using Lodash Library
  • Using some() Method

Similar Reads

Using Set

This approach uses the Set data structure in JavaScript, which automatically removes duplicate values. By mapping the array of objects to their JSON representations, the Set eliminates duplicate string representations, and then the unique objects are reconstructed....

Using filter() and indexOf() methods

The approach uses filter() and indexOf() to create a unique array by checking the index of each object’s first occurrence based on JSON string representation for precise comparison. This ensures the removal of duplicate objects from the original array....

Using Map

This approach uses a Map to efficiently remove duplicate objects from an array. It iterates through the input array, converts each object to a string using JSON.stringify as the key, and checks if the Map already has that key. If not, it adds the key to the Map and pushes the corresponding object to the unique array. This method ensures that unique objects are retained in the final array....

Using reduce()

The below approach uses the reduce() method to create a unique array of objects by comparing each object’s string representation. The accumulator (acc) is updated only if the current object is not already present in the accumulator, effectively removing duplicates based on their stringified form....

Using Lodash Library

Using the Lodash library’s uniqWith function with a custom comparison function compares objects for uniqueness. It returns an array of unique objects by comparing their properties or values, allowing for flexible and efficient handling of object uniqueness....

Using some() Method

This approach uses the some() method to create a unique array of objects. The some() method checks if at least one element in the array passes the test implemented by the provided function. If an object with the same key already exists in the unique array, it is not added again....