I have an array with objects and each contain date in the format : day ' month ' year
Here is the example of single object in that array
{
"title": "“All’Improvviso” - Cajo Fabricio",
"artist": "Johann Adolf Hasse",
"link": "https://www.facebook.com/AllimprovvisoFestivalGliwice",
"day": "sobota, godz. 18:00",
**"date": "26 ' 06 ' 2021",**
"place": "Ruiny Teatru Victoria",
"city": "Gliwice",
"canceled": false,
"ticketSoon": false
}
I want to create a new array from this objects with the descending order of a date.
Can anyone help? I am making the calculations in Nuxt project
Parse the string as date and then sort the array in order of date
const data = [{
"artist": "Johann Adolf Hasse",
"date": "26 ' 06 ' 2021",
}, {
"artist": "Test Hasse",
"date": "23 ' 06 ' 2021",
}];
const sortedData = data.map(item => {
const splitDate = item.date.split("'");
const newDate = new Date(splitDate[2].trim(), splitDate[1].trim(), splitDate[0].trim());
return { ...item,
date: newDate
}
}).sort((a, b) => {
return new Date(a.date) - new Date(b.date)
})
console.log(sortedData)