-2
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

1
  • Why do you want to do this in an async method? And other posters answered your title question but not the question you asked below your code. Can you clarify what you would like to have answered? Commented May 3, 2019 at 15:14

2 Answers 2

1

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;
});
Sign up to request clarification or add additional context in comments.

Comments

0
const filtered = values.filter(ele => ele.name == "YOUR_CONDITION");
if(filtered.length > 1){
  // array contains duplicate elements.
}

1 Comment

Please, add some description.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.