I have an array of objects I am trying to find duplicates based on some properties (first and last). My logic appears to be off and here is what I have tried.
My final result should look similar to:
[
{first:"John", last: "Smith", id:"1234", dupes: [555,666]},
{first:"John", last: "Jones", id:"333", dupes: []}
];
let arrayOfObjects =
[
{first:"John", last: "Smith", id:"1234", dupes: []},
{first:"John", last: "Smith", id:"555", dupes: []},
{first:"John", last: "Jones", id:"333", dupes: []},
{first:"John", last: "Smith", id:"666", dupes: []}
];
arrayOfObjects.forEach(record => {
arrayOfObjects.forEach(rec => {
if(record.first == rec.first &&
record.last == rec.last &&
record.id !== rec.id){
console.log("match found for: " + JSON.stringify(record) + " and: " + JSON.stringify(rec));
record.dupes.push(rec.id);
//probably need to remove something here
}
});
});
console.log(JSON.stringify(arrayOfObjects));