close

How can I check if the array of objects have duplicate property values?

Hello Guys, How are you all? Hope You all Are Fine. Today we are going to learn How can I check if the array of objects have duplicate property values? in javascript. So Here I am Explain to you all the possible methods here.

Without wasting your time, Let’s start This Article.

How can I check if the array of objects have duplicate property values ?

  1. How can I check if the array of objects have duplicate property values ?

    can I check if the array of objects have duplicate property values You can use map to return just the name, and then use this forEach trick to check if it exists at least twice:

  2. can I check if the array of objects have duplicate property values

    can I check if the array of objects have duplicate property values You can use map to return just the name, and then use this forEach trick to check if it exists at least twice:

Method 1

ECMA Script 6 Version

If you are in an environment which supports ECMA Script 6’s Set, then you can use Array.prototype.some and a Set object, like this

let seen = new Set();
var hasDuplicates = values.some(function(currentObject) {
    return seen.size === seen.add(currentObject.name).size;
});

Here, we insert each and every object’s name into the Set and we check if the size before and after adding are the same. This works because Set.size returns a number based on unique data (set only adds entries if the data is unique). If/when you have duplicate names, the size won’t increase (because the data won’t be unique) which means that we would have already seen the current name and it will return true.


ECMA Script 5 Version

If you don’t have Set support, then you can use a normal JavaScript object itself, like this

var seen = {};
var hasDuplicates = values.some(function(currentObject) {

    if (seen.hasOwnProperty(currentObject.name)) {
        // Current name is already seen
        return true;
    }

    // Current name is being seen for the first time
    return (seen[currentObject.name] = false);
});

The same can be written succinctly, like this

var seen = {};
var hasDuplicates = values.some(function (currentObject) {
    return seen.hasOwnProperty(currentObject.name)
        || (seen[currentObject.name] = false);
});

Note: In both the cases, we use Array.prototype.some because it will short-circuit. The moment it gets a truthy value from the function, it will return true immediately, it will not process rest of the elements.

Method 2

You can use map to return just the name, and then use this forEach trick to check if it exists at least twice:

var areAnyDuplicates = false;

values.map(function(obj) {
    return obj.name;
}).forEach(function (element, index, arr) {
    if (arr.indexOf(element) !== index) {
        areAnyDuplicates = true;
    }
});

Summery

It’s all About this article. Hope all method helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you? Thank You.

Leave a Comment