How can I check if the array of objects have duplicate property values?
Use array.prototype.map and array.prototype.some:
var values = [ { name: 'someName1' }, { name: 'someName2' }, { name: 'someName4' }, { name: 'someName2' }];var valueArr = values.map(function(item){ return item.name });var isDuplicate = valueArr.some(function(item, idx){ return valueArr.indexOf(item) != idx });console.log(isDuplicate);
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.
In TS and ES6 you can create a new Set with the property to be unique and compare it's size to the original array.
const values = [ { name: 'someName1' }, { name: 'someName2' }, { name: 'someName3' }, { name: 'someName1' }]const uniqueValues = new Set(values.map(v => v.name));if (uniqueValues.size < values.length) { console.log('duplicates found')}