In this article, we are going to see how to get duplicate object from array in JavaScript? Okay, this is the JavaScript problem statement which we are going to discuss, and also we will solve it in very simple and straight-forward way.
As we know, an object array can contain a number of elements and one key which we can say identity of the object should be unique. So this issue arises when identity key is not unique anymore. For example, if we have an array of objects, in which we can put multiple objects, but let’s say ID parameter should be unique but array itself won’t deny same ID objects. So it is possible that there will be multiple IDs which are the same. In this situation, accessing a particular object becomes hard.
const array = [
{id: 1, name: "mac"},
{id: 2, name: "alex"},
{id: 1, name: "allen" },
{id: 3, name: "ross"},
{id: 1, name: "alex"},
];
As you can see, in the above code we have multiple objects, and also we have some duplicate objects as well with duplicate IDs. Here, if we want to access certain object then we consider the ID which should be unique but here we have some duplicate IDs, so we will get multiple object instead of one.
Using Reduce Method
To solve such kind of problems, we need some sort of solutions to filter out our array where only unique key should be there. For that, we have multiple ways to perform this operation, but we will consider a very optimized and straight way to solve this problem.
We will use reduce method, as its name suggests, it will reduce the array as per over logic. So here we have an array as above, then we have created a constant to store the new array. In this, we have applied to reduce method, to get filter result. Here in reduce((final_array, current)=>{})
, final_array is a temporary array to store value, and lastly we will store this array in new_array. Then current parameter refers to current element, if works as loop, and its value will be changed on each rotation. Basically, this element will store that object on which we are currently working.
Now in this callback function, we have created a variable named obj to store a boolean value true or false. For that, we have initiated final_array.find((item)=>item.id === current.id)
which returns true or false. We have applied find method on final_array to find similar id from existing array. Let’s say at first time final_array will be empty, so item has no value then obj becomes false, and we have concatenated the current object if obj is false. At third time, where current’s object ID will be similar to first object, at that time, obj will be true, and we won’t concat that object in our final_array. So basically, we will only concat those objects who has unique IDs.
const array = [
{id: 1, name: "mac"},
{id: 2, name: "alex"},
{id: 1, name: "allen" },
{id: 3, name: "ross"},
{id: 1, name: "alex"},
];
const new_array = array.reduce((final_array, current)=>{
let obj = final_array.find((item)=>item.id === current.id);
if(obj){
return final_array;
}else{
return final_array.concat(current);
}
},[]);
console.log(new_array);
Note: Here, array runs on sequentially, so those value who has duplicate IDs won’t show up in new array. As you can see, 3rd and 5th object has the same ID as 1st object, so 3rd and 5th won’t be placed in a new array.