var values = {[
    { name: 'jerem' },
    { name: 'jio' },
    { name: 'paul' },
    { name: 'jio' }}
];

var valueArr = values.map(function(item){ return item.name });
var isDuplicate = valueArr.some(function(item, idx){ 
    return valueArr.indexOf(item) != idx 
});
console.log(isDuplicate);

How to write the above code using promise all and async? can someone help me here

2 Answers

0
Swarup Bam On Best Solutions
const filtered = values.filter(ele => ele.name == "YOUR_CONDITION");
if(filtered.length > 1){
  // array contains duplicate elements.
}
0
Arvind Rajput On

There are lots of solutions to find the duplicate objects from an array

Solution 1:

 var values = [
    { name: 'someName1' },
    { name: 'someName2' },
    { name: 'someName1' },
    { name: 'someName1' }
]

// solution
var hasDuplicate = false;
values.map(v => v.name).sort().sort((a, b) => {
  if (a === b) hasDuplicate = true
})
console.log('hasDuplicate', hasDuplicate)

Solution 2nd: 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;
});