tl;dr
Im trying to deduplicate an array of objects that are not necessarily identical.
desc.
i have downloaded a set of records from a site that manages them poorly and there are many duplicates of records, though some with different names or different weights. What id like to do is compare each record to each other record and remove all entries but the one with the greatest weight, or all but one entry in the case that there is an exact duplicate (including name).
Arr obj
{
"entry" : "Single Lift",
"name" : "Jane Doe",
"sex" : "female",
"division": "40-44",
"equipped": "raw",
"wtclass" : 66,
"lift" : "bench press",
"weight" : 151
}
Pseudo
function dedupe(lift=records){
console.log(lift.length)
lift.forEach((record,index)=>{
for(i=0;i<lift.length;i++){
if(record.sex===lift[i].sex){
if(record.division===lift[i].division){
if(record.equipped===lift[i].equipped){
if(record.wtclass===lift[i].wtclass){
if(record.entry===lift[i].entry){
if(record.name===lift[i].name&&record.weight===lift[i].weight) lift.splice(i,1)
else if(record.weight>lift[i].weight) lift.splice(i,1)
else if(record.weight<lift[i].weight) lift.splice(index,1)
}
}
}
}
}
}
})
console.log(lift.length)
return lift
}
This code obviously doesn't operate the way i want it to, but it illustrates the intent. I would like to remove all but the greatest weight (or all but one where all key:values are identical) by comparing the weight on any entry where entry sex division equipped wtclass and lift are a match.
There are other questions around about deduplicating arrays of objects, but i could not find anything that matched my scenario or that i could adapt to work for me. If anyone has a solution (or a link to a solution) i would greatly appreciate it!