Open In App

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

Last Updated : 07 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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:

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.

Syntax:

let mySet = new Set();

Example: Using Set to create a unique array of objects by stringifying and parsing their JSON representations, eliminating duplicates based on content.

Javascript




let arr = [
  { id: 1, name: "Geek1" },
  { id: 2, name: "Geek2" },
  { id: 1, name: "Geek1" },
  { id: 3, name: "Geek3" },
];
 
// unique objects using Set
let uniqueArr =
    [...new Set(arr.map(JSON.stringify))].map(JSON.parse);
 
// 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' },
...

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.

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' },
...

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.

Syntax:

let newArray = originalArray.map((currentValue, index, array) => {
// code
});

Example: Removing duplicate objects from the input array using a Map to track unique object keys based on their JSON string representation, resulting in a unique array.

Javascript




let arr = [
  { id: 1, name: "Geek1" },
  { id: 2, name: "Geek2" },
  { id: 1, name: "Geek1" },
  { id: 3, name: "Geek3" },
];
let map = new Map();
 
// Removing duplicate objects using map
let uniqueArr = [];
arr.forEach((obj) => {
  const key = JSON.stringify(obj);
  if (!map.has(key)) {
    map.set(key, true);
    uniqueArr.push(obj);
  }
});
 
// 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' },
...

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.

Syntax:

let result = array.reduce((accumulator, currentValue, index, array) => {
//code
}, initialValue);

Example: Using the reduce method, this approach iterates through the input array, checking for duplicate objects based on their stringified representations.

Javascript




let arr = [
  { id: 1, name: "Geek1" },
  { id: 2, name: "Geek2" },
  { id: 1, name: "Geek1" },
  { id: 3, name: "Geek3" },
];
 
// Removing objects using reduce()
let uniqueArr = arr.reduce((acc, obj) => {
  const existingObj = acc.find(
    (item) => JSON.stringify(item)
        === JSON.stringify(obj)
  );
  if (!existingObj) {
    acc.push(obj);
  }
  return acc;
}, []);
 
// 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' },
...


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads