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>
CodePudding user response:
You should use custom sort function as:
datas.sort((a, b) => a.date - b.date)
There is no need to use find when you are reverseing the array and getting the index 1 from it.
Note: I deliberately change the order of the datas array
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: 4,
date: moment(String('Honeydew - 30112021').match(/[0-9]/g).join(""), 'DDMMYYYY').toDate()
},
{
id: 3,
date: moment(String('Lemon & Oranges - 30102021').match(/[0-9]/g).join(""), 'DDMMYYYY').toDate()
}
];
const secondLatestDate = datas.sort((a, b) => a.date - b.date).reverse()[1];
console.log(secondLatestDate);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
or you can directly find the second largest after sort. There is no need to reverse the array
datas.sort((a, b) => a.date - b.date)[datas.length - 2]
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: 4,
date: moment(
String('Honeydew - 30112021').match(/[0-9]/g).join(''),
'DDMMYYYY'
).toDate(),
},
{
id: 3,
date: moment(
String('Lemon & Oranges - 30102021').match(/[0-9]/g).join(''),
'DDMMYYYY'
).toDate(),
},
];
const secondLatestDate = datas.sort((a, b) => a.date - b.date)[datas.length - 2];
console.log(secondLatestDate);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
CodePudding user response:
If you're at all concerned with time complexity you could do this in one 'n' by implementing a function that keeps track of both the highest and second highest values. Maybe something like this:
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: 4,
date: moment(String('Honeydew - 30112021').match(/[0-9]/g).join(""), 'DDMMYYYY').toDate()
},
{
id: 3,
date: moment(String('Lemon & Oranges - 30102021').match(/[0-9]/g).join(""), 'DDMMYYYY').toDate()
}
]
function getSecondHighest(dates){
let highest = 0;
let secondHighest = 0;
for(const val of dates){
const currentDate = val.date
if(currentDate > highest){
secondHighest = highest;
highest = currentDate;
} else if(currentDate > secondHighest){
secondHighest = currentDate
} else {
continue;
}
}
return secondHighest;
}
