I am looking to eliminate duplicate object entries based on id, while displaying the most recent object entry based on pubDate. Take a look at my example array below:
var arr = [
{fileID: "1234", pubDate: "04/13/2018", id: "4979146", jobID: "9146", downloadURL: "", title: null},
{fileID: "1235", pubDate: "04/13/2020", id: "4979147", jobID: "9147", downloadURL: "", title: null},
{fileID: "1236", pubDate: "02/23/2021", id: "4979148", jobID: "9148", downloadURL: "", title: null},
{fileID: "1237", pubDate: "01/15/2021", id: "4979148", jobID: "9148", downloadURL: "", title: null},
{fileID: "1238", pubDate: "05/17/2019", id: "4979146", jobID: "9146", downloadURL: "", title: null}
];
Desired Output:
[
{fileID: "1236", pubDate: "02/23/2021", id: "4979148", jobID: "9148", downloadURL: "", title: null},
{fileID: "1235", pubDate: "04/13/2020", id: "4979147", jobID: "9147", downloadURL: "", title: null},
{fileID: "1238", pubDate: "05/17/2019", id: "4979146", jobID: "9146", downloadURL: "", title: null}
];
This is how I attempted to solve it:
var result = arr.reduce((unique, o) => {
if (!unique.some(obj => obj.id === o.id)) {
unique.push(o);
}
return unique;
}, []);
... however, this method appears to be missing the logic to retain only the latest record.