Skip to content
the poor coder

remove duplicate object from array javascript

Poor Man
Published
Reading time
2 min read
On this page

Remove Duplicate Object from Array in JavaScript#

As a programmer, I often come across the situation where I need to remove duplicate objects from an array using JavaScript. Let me share with you the solution I found that worked for me.

Using Set#

The easiest and most efficient way to remove duplicate objects from an array is to use a Set object in JavaScript. Here is the code to do so:

Text

const arr = [{id: 1, name: 'John'}, {id: 2, name: 'Jane'}, {id: 1, name: 'John'}, {id: 3, name: 'James'}];

const uniqueArr = [...new Set(arr.map(item => JSON.stringify(item)))].map(item => JSON.parse(item));

console.log(uniqueArr);

In the above code, we are creating a Set object from the array of objects and then converting it back to an array using the spread operator. We are using JSON.stringify and JSON.parse methods to compare the objects by their string representation.

Using Filter and IndexOf#

Another way to remove duplicate objects from an array is to use the filter method and indexOf method. Here is the code:

Text

const arr = [{id: 1, name: 'John'}, {id: 2, name: 'Jane'}, {id: 1, name: 'John'}, {id: 3, name: 'James'}];

const uniqueArr = arr.filter((item, index) => {
  return index === arr.findIndex(obj => JSON.stringify(obj) === JSON.stringify(item));
});

console.log(uniqueArr);

In the above code, we are using the filter method to filter out the objects from the array that do not meet the condition of having a unique string representation. We are using the findIndex method to get the index of the first occurrence of the object with the same string representation and then comparing it with the current index using the indexOf method.

Using Reduce and Some#

One more way to remove duplicate objects from an array is to use the reduce method and some method. Here is the code:

Text

const arr = [{id: 1, name: 'John'}, {id: 2, name: 'Jane'}, {id: 1, name: 'John'}, {id: 3, name: 'James'}];

const uniqueArr = arr.reduce((acc, item) => {
  if (!acc.some(obj => JSON.stringify(obj) === JSON.stringify(item))) {
    acc.push(item);
  }
  return acc;
}, []);

console.log(uniqueArr);

In the above code, we are using the reduce method to iterate over the array of objects and accumulate unique objects in a new array. We are using the some method to check if the object with the same string representation already exists in the accumulator array.

These are three ways to remove duplicate objects from an array using JavaScript. You can choose the one that suits your needs and coding style.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC