I have a problem getting the second highest date in ES6. I'm using moment.js too.
Its supposed to be getting the id of 3.
const datas = [
{
id: 1,
date: moment(String('Apple & Banana - 20072021').match(/[0-9]/g).join(""), 'DDMMYYYY').toDate()
},
{
id: 2,
date: moment(String('Apple & Oranges - 30082021').match(/[0-9]/g).join(""), 'DDMMYYYY').toDate()
},
{
id: 3,
date: moment(String('Lemon & Oranges - 30102021').match(/[0-9]/g).join(""), 'DDMMYYYY').toDate()
},
{
id: 4,
date: moment(String('Honeydew - 30112021').match(/[0-9]/g).join(""), 'DDMMYYYY').toDate()
}
];
const secondLatestDate = new Date(datas.map(file => new Date(file.date)).sort().reverse()[1]);
const finalResult = datas.find(file => file.date.getTime() === secondLatestDate.getTime());
console.log(finalResult)
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>