5 ways to remove duplicate objects from an array based on a property

Last updated on June 21, 2022

The other day I was working on a small side project where I retrieve tweets and display them in a grid. In the hero section, I wanted to display the authors of the tweets so people can easily scan them.

But what happens when I share multiple tweets by the same person? I would end up showcasing the avatar of the person multiple times as well.

I wanted to find a way to remove the duplicate avatars and only display each other once, no matter how many tweets from that person. Let's explore different technique to get this done.

Our test data

Here's a simplified version of the data that I receive from my API call.

We would like to remove the duplicate authors and then loop through the data and display only the unique authors.

javascript
1
const data = [
2
{ tweet: { id: '101', author: { id: '1', name: 'Michael' } } },
3
{ tweet: { id: '102', author: { id: '2', name: 'Gob' } } },
4
{ tweet: { id: '103', author: { id: '1', name: 'Michael' } } },
5
{ tweet: { id: '104', author: { id: '3', name: 'Buster' } } },
6
{ tweet: { id: '105', author: { id: '2', name: 'Gob' } } },
7
];

Our goal is to get to the following array which will allow us to simply loop through the array and display the authors.

javascript
1
const idealOutcome = [
2
{ tweet: { id: '101', author: { id: '1', name: 'Michael' } } },
3
{ tweet: { id: '102', author: { id: '2', name: 'Gob' } } },
4
{ tweet: { id: '104', author: { id: '3', name: 'Buster' } } },
5
];

Technique #1: Use a reduce method

We can use the reduce method to go through every item and see if we already have an object added to the accumulator with the same author id as the current item.

javascript
1
const uniqueAuthors = data.reduce((accumulator, current) => {
2
if (!accumulator.find((item) => item.tweet.author.id === current.tweet.author.id)) {
3
accumulator.push(current);
4
}
5
return accumulator;
6
}, []);
7
8
9
console.log(uniqueAuthors.map(item => item.tweet.author.id))
10
// ["1", "2", "3"]
11
console.log(idealOutcome.map(item => item.tweet.author.id))
12
// ["1", "2", "3"]

Technique #2: Use the Map object

Using the Map object is a more performant technique and takes less lines of code to write, but it could not preserve the order of the original array and it might not be as readable for other developers.

javascript
1
const uniqueAuthors = [...new Map(data.map(v => [v.tweet.author.id, v])).values()]
2
3
4
console.log(uniqueAuthors.map(item => item.tweet.author.id))
5
// ["1", "2", "3"]
6
console.log(idealOutcome.map(item => item.tweet.author.id))
7
// ["1", "2", "3"]

Technique #3: Use lodash.uniqueWith()

If you are already using Lodash, uniqueWith() function might be a suitable and fast solution for you.

javascript
1
import _ from "lodash"
2
3
const uniqueAuthors = _.uniqWith(data, (arrVal, othVal) => arrVal.tweet.author.id === othVal.tweet.author.id)
4
5
6
console.log(uniqueAuthors.map(item => item.tweet.author.id))
7
// ["1", "2", "3"]
8
console.log(idealOutcome.map(item => item.tweet.author.id))
9
// ["1", "2", "3"]

Technique #4: Filter + Set()

Another technique is to use the Set object and its add and has methods.

This is subjective opinion, but this is probably the most readable solution out of all other techniques in this article.

javascript
1
const seen = new Set();
2
const uniqueAuthors = data.filter(item => {
3
const duplicate = seen.has(item.tweet.author.id);
4
seen.add(item.tweet.author.id);
5
return !duplicate;
6
});
7
8
9
console.log(uniqueAuthors.map(item => item.tweet.author.id))
10
// ["1", "2", "3"]
11
console.log(idealOutcome.map(item => item.tweet.author.id))
12
// ["1", "2", "3"]

Technique #5: Filter + findIndex

A similar approach to Technique #4, but this time we are using the findIndex method. This is probably the least performant method out all since you are doing a loop inside a loop.

javascript
1
const uniqueAuthors = data.filter((value, index, self) =>
2
index === self.findIndex((t) => (
3
t.tweet.author.id === value.tweet.author.id
4
))
5
)
6
7
8
console.log(uniqueAuthors.map(item => item.tweet.author.id))
9
// ["1", "2", "3"]
10
console.log(idealOutcome.map(item => item.tweet.author.id))
11
// ["1", "2", "3"]

Do you have any other techniques which you'd like to share? Drop a comment below and let us know!

Invite us to your inbox.

Articles, guides and interviews about web development and career progression.

Max 1-2x times per month.