mercredi 30 septembre 2020

how can I minimize code function that find repeated values in Array of Objects in JavaScript

I need to fix this function, which must find two similar names in an array of object. I tried to do this, and it's work, but the test tells me that should be just only one loop and one if

function searchByName() {
    const values = [
        { name: 'Johny Walker', birthDate: '1995-12-17' },
        { name: 'Andrew', birthDate: '2001-10-29' },
        { name: 'Viktor', birthDate: '1998-11-09' },
        { name: 'Andrew', birthDate: '2011-05-09' }
    ];

    for (let obj of values) {
        for (let elem of values) {
            if (obj == elem)
                continue;
            if (elem.name === obj.name && elem.age === obj.age) {
                console.log(obj);
                break;
            }
        }
    }

};

here is the example that must come out

[
  { name: 'Andrew', birthDate: '2001-10-29' },
  { name: 'Andrew', birthDate: '2011-05-09' }
]

Aucun commentaire:

Enregistrer un commentaire