Thursday, May 23, 2024
 Popular · Latest · Hot · Upcoming
70
rated 0 times [  76] [ 6]  / answers: 1 / hits: 14983  / 4 Years ago, thu, january 2, 2020, 12:00:00

Hi I have list of values in array as bellow



    var users = [{
name: 'John',
email: '[email protected]',
age: 25,
},
{
name: 'Tom',
email: '[email protected]',
age: 35,
},
{
name: 'John',
email: '[email protected]',
age: 25,
}];


I should find duplicates row from the above array (need to compare all the fields name, email, and age)



I have used some function to find a duplicate value as below but need to pass multiple conditions in it. How to do that



 const unique    = new Set();

const showError = this.users.some(element => unique.size === unique.add(element.name).size);


As I have passed the name I need to verify email and age. How to do that


More From » arrays

 Answers
14

Maintain counter while checking equality, as every object will be equal with same object, hence check if counter is greater than 1 for getting status.



const status = users.some(user => {
let counter = 0;
for (const iterator of users) {
if (iterator.name === user.name && iterator.email === user.email && iterator.age === user.age) {
counter += 1;
}
}
return counter > 1;
});

[#5184] Sunday, December 29, 2019, 5 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
braeden

Total Points: 231
Total Questions: 96
Total Answers: 86

Location: Somalia
Member since Mon, Dec 28, 2020
3 Years ago
braeden questions
Fri, Oct 29, 21, 00:00, 3 Years ago
Thu, Oct 10, 19, 00:00, 5 Years ago
Thu, Mar 21, 19, 00:00, 5 Years ago
;