I have a array of object in that I want to find most recent date.After getting most recent date then I want to add new field within that object and return the array. I checked it out some stackoverflow question but didn't worked for me,If anyone have any idea please guide me.
e.g:
let myArray = [
{id: "12ad5e2as", userName: "john", createdAt : 1592638519388},
{id: "52se5e2as", userName: "Herry", createdAt : 1592565371062},
{id: "25asd5e5e", userName: "Lisa", createdAt : 1592638177825},
{id: "15ease55e", userName: "Mona", createdAt : 1586505563915}
]
after finding most recent date or greater date then I want to add new field and myArray will look like this.
myArray = [
{id: "12ad5e2as", userName: "john", createdAt : 1592638519388},
{id: "52se5e2as", userName: "Herry", createdAt : 1592565371062},
{id: "25asd5e5e", userName: "Lisa", createdAt : 1592638177825,
isMostRecent: true},
{id: "15ease55e", userName: "Mona", createdAt : 1586505563915}
]
I tried to apply sort function but its adding isMostRecent: true to every object. Can someone please guide me how can I do that?
here is what I have tried.
myArray.sort((a, b) => {
if (a.createdAt > b.createdAt)
a.isNew = true;
return -1;
})
Any help will be appreciated.