# Remove duplicate objects from an array.

It’s a common use case in projects to remove duplicates from an array of objects in JavaScript and It’s helpful for coding challenges to practice.

We are given an **array** of **objects** and the task is to remove the duplicate object element from the array list.

In this article. We will see multiple ways to remove duplicates from an array of objects.

### The following way -

* **Higher-order functions(map, filter, reduce)**
    
* **iterative way**
    
* using **Set** object
    
* using **Map** object
    

```json
const array = [
    {"id": 5, "name": "Johnson"}, {"id": 4, "name": "John"},
    {"id": 3, "name": "Juliet"}, {"id": 2, "name": "Nancy"},
    {"id": 5, "name": "Johnson"}, {"id": 5, "name":"Johnson"},
    {"id": 2, "name": "Nancy"}, {"id": 3, "name": "Juliet"}
];
```

### **Higher-order functions**

* [`Array.prototype.map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map)`()`
    

```javascript
const uniqueData = [...new Map(array.map(item => [item.id, item])).values()];
console.table(uniqueData);
```

* [`Array.prototype.filter()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter)
    

```javascript
const uniq = {};
const uniqueData = array.filter(obj => {
  return !uniq[obj.id] && (uniq[obj.id] = true)
});
console.table(uniqueData);
```

* [`Array.prototype.reduce()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce)
    

```javascript
let uniqueData = array.reduce((arr, item) => {
   const filtered = arr.filter(i => i['id'] !== item['id']);
   return [...filtered, item];
}, []);

console.table(uniqueData);
```

> *To get output in tabular data as a table you can use* [***console.table()***](https://developer.mozilla.org/en-US/docs/Web/API/Console/table)

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690478939392/7e760ac4-e1c6-414e-8514-075c8d53766d.png align="center")

### **Iterative**

```javascript
function unique(array) {
   const uniqueArr = [];
   const uniqueObject = {};

    for (let i in array) {
       objTitle = array[i].name;
       uniqueObject[objTitle] = array[i];
    }
    
    for (let i in uniqueObject) {
       uniqueArr.push(uniqueObject[i]);
    }
    
   console.table(uniqueArr);
}
unique(array);
```

> Another way

```javascript
const result = {};
for (let i = 0, n = array.length; i < n; i++) {
   result[array[i].name] = array[i];
}

let unique = [], index = 0;
for(let item in result) {
   unique[index++] = result[item];
}
console.table(unique);
```

### **Using** `Set`

The [`Set`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set)object lets you store unique values of any type, whether [primitive values](https://developer.mozilla.org/en-US/docs/Glossary/Primitive) or object references.

```javascript
function unique(arr) {
    const jsonObject = arr.map(JSON.stringify);
    uniqueSet = new Set(jsonObject);
    uniqueArray = Array.from(uniqueSet).map(JSON.parse);
    
    console.table(uniqueArray);
}
unique(array);
```

### **Using** `Map`

The [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map/Map) object holds key-value pairs and remembers the original insertion order of the keys. Any value (both objects and [primitive values](https://developer.mozilla.org/en-US/docs/Glossary/Primitive)) may be used as either a key or a value.

```javascript
const uniqueData = [];
const map = new Map();
for (const object of array) {
   if (!map.has(object.id)) {
        map.set(object.id, true);
        uniqueData.push({
            id: object.id,
            name: object.name
        });
    }
}
console.table(uniqueData);
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1690479684414/740e455c-b6d7-468f-8ffc-47d6e2d25e8e.png align="center")

![](https://cdn-images-1.medium.com/max/800/1*rmZC4q5GQsV8nWzckOcx4g.png align="left")

That’s all! I hope this article might be helpful to remove duplicates from an array of objects.

Thanks for reading ❤.

Say Hello! [**Twitter**](https://twitter.com/akhtarvahid) **|** [**Blog**](https://medium.com/@akhtarvahid) **|** [**LinkedIn**](https://www.linkedin.com/in/akhtarvahid/) **|** [**Github**](https://github.com/akhtarvahid)

---

### Links and Resources

* [**Polyfill for bind**](https://akhtarvahid.hashnode.dev/polyfill-for-bind)
    
* [**Deep-copying objects in JavaScript**](https://akhtarvahid.hashnode.dev/deep-copying-objects-in-javascript)
    
* [**Flatten nested array in javascript**](https://akhtarvahid.hashnode.dev/flatten-nested-array)
    
* [**CRUD Application in React.js with Redux and Hooks**](https://akhtarvahid.medium.com/crud-application-in-react-js-with-redux-and-hooks-e0367b6aeeb2).
    
* [**How to access/fetch the local JSON file to React.**](https://akhtarvahid.medium.com/how-to-access-fetch-the-local-json-file-to-react-5ce07c43731d)
