[
{
uid: "1",
owner: "2"
createdAt: Date
}, {
uid: "2",
owner: "1"
createdAt: Date
},{
uid: "3",
owner: "2"
createdAt: Date
},
]
function removeSomeShit(arr) {
const table = {};
const result = [];
for(const current of arr) {
const {uid, owner, createdAt} = current;
const uidOvnerKey = `${uid}\0${owner}`;
const ownerUidKey = `${owner}\0${uid}`;
const matched = table[uidOvnerKey];
if (matched) {
delete table[uidOvnerKey];
result.push(matched.createdAt > createdAt ? matched : current);
} else {
table[ownerUidKey] = current;
}
};
return Object.values(table).concat(result);
}