I am currently working with an array of objects where I need to identify duplicates based on specific properties (first and last names). Below is my attempt at solving this issue:
The expected output should resemble:
[
{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));