【问题标题】:Get the Second Highest Date in JavaScript/ES6在 JavaScript/ES6 中获取第二大日期
【发布时间】:2022-01-16 03:02:20
【问题描述】:
在 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>
【问题讨论】:
标签:
javascript
ecmascript-6
momentjs
【解决方案1】:
您应该使用自定义排序功能:
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>
【解决方案2】:
如果您完全关心时间复杂度,您可以通过实现一个跟踪最高值和第二高值的函数在一个“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;
}