在JavaScript/ES6中获得第二高日期



我在ES6中获取第二高日期时遇到问题。我也在使用moment.js。它应该得到3的id

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>

您应该使用自定义排序函数作为:

datas.sort((a, b) => a.date - b.date)

当您对数组进行reverse操作并从中获取索引1时,不需要使用find

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>

或者你可以直接找到排序后的第二大。不需要reverse数组

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>

如果你关心时间复杂性,你可以通过实现一个跟踪最高值和第二高值的函数,在一个"n"中完成这项工作。也许是这样的:


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;
}

最新更新