I need to get a recent entry among from multiple entries.
let items = [{Fruit: "Apple",date: 1592809121000},
{Fruit: "Orange",date: 1592564167000},
{Fruit: "Apple",date: 1592566351000},
{Fruit: "Orange",date: 1592809121000},
{Fruit: "Apple",date: 1592564167000},
{Fruit: "Apple",date: 1593153998000},
{Fruit: "Orange",date: 1592893249000},
{Fruit: "Grapes",date: 1592565214000}]
Expected output:
let items = [{
Fruit: "Apple",
date: recent entry
}, {
Fruit: "Orange",
date: recent entry
}, {
Fruit: "Grapes",
date: recent entry
}]
Tried code to sort with date but it is sorting among all fruits but i want to sort only among specific fruit to get latest entry with timestamp and remove other entries from array
// Sort by date and show recent first
items = items.sort(function(a, b) {
return (new Date(b.date))- (new Date(a.date));
});