gpt4 book ai didi

javascript - 查找数组中最近的日期

转载 作者:行者123 更新时间:2023-12-03 00:11:44 25 4
gpt4 key购买 nike

我试图在具有不同日期的对象数组中找到最高日期,这些日期将归因于每个对象。只要日期在 1970 年 1 月 1 日以上,代码就可以正常工作,但在此之前的任何日期都会导致错误。我该如何解决这个问题并保持相同的日期?我知道我可以在所有日期上使用 get(Time) 但有什么办法解决这个问题吗?谢谢。

var playerData = [
{name: "John"},
{name: "Bill"},
{name: "Bob"},
{name: "Jim"},

];

var dateOne = new Date(1940,02,05);
var dateTwo = new Date(1950, 06,18);
var dateThree = new Date(1650,07,12);
var dateFour = new Date(1300, 03,25);

playerData[0].date = dateOne;
playerData[1].date = dateTwo;
playerData[2].date = dateThree;
playerData[3].date = dateFour;

function findHighDate() {
var highDateSoFar = null;
var result;
for (var i = 0; i < playerData.length; i++) {
if (playerData[i].date > highDateSoFar) {
result = playerData[i];
highDateSoFar = playerData[i].date;
}
else if (playerData[i].date === highDateSoFar) {
result = 'equal';

}
}
return result;
}

var highPlayer = findHighDate();
var highPlayerName = highPlayer.name;
var highPlayerIndex = playerData.indexOf(highPlayer);
var highPlayerDate = highPlayer.date;
console.log({highPlayer},{highPlayerIndex},{highPlayerName},{highPlayerDate});

最佳答案

您可以比较字符串并使用 Array.sort 按降序对它们进行排序,然后获取第一项,而不是比较日期:

const playerData = [
{name: "John", date: '1940-02-05' },
{name: "Bill", date: '1950-06-18' },
{name: "Bob", date: '1650-07-12' },
{name: "Jim", date: '1300-03-25' },
];

function findHighDate() {
return playerData.sort((a, b) => b.date.localeCompare(a.date))[0];
}

const highPlayer = findHighDate();
const highPlayerName = highPlayer.name;
const highPlayerIndex = playerData.indexOf(highPlayer);
const highPlayerDate = new Date(highPlayer.date);
console.log({ highPlayer, highPlayerIndex, highPlayerName, highPlayerDate });

或者您也可以坚持使用日期并使用 Date.getTime() 比较它们来对数组进行排序:

const playerData = [
{name: "John", date: new Date('1940-02-05') },
{name: "Bill", date: new Date('1950-06-18') },
{name: "Bob", date: new Date('1650-07-12') },
{name: "Jim", date: new Date('1300-03-25') },
];

function findHighDate() {
return playerData.sort((a, b) => b.date.getTime() - a.date.getTime())[0];
}

const highPlayer = findHighDate();
const highPlayerName = highPlayer.name;
const highPlayerIndex = playerData.indexOf(highPlayer);
const highPlayerDate = highPlayer.date;
console.log({ highPlayer, highPlayerIndex, highPlayerName, highPlayerDate });

正如 @Scott Sauyet 在下面的评论中指出的那样,使用 Array.sort 对于您的场景来说可能有点过分了。

您可以使用更多代码并在 reduce 的帮助下找到最高日期:

const playerData = [
{name: "John", date: new Date('1940-02-05') },
{name: "Bill", date: new Date('1950-06-18') },
{name: "Bob", date: new Date('1650-07-12') },
{name: "Jim", date: new Date('1300-03-25') },
];

function findHighDate() {
return playerData.reduce((highest, player) => {
return highest.date.getTime() > player.date.getTime() ? highest : player;
}, playerData[0]);
}

const highPlayer = findHighDate();
const highPlayerName = highPlayer.name;
const highPlayerIndex = playerData.indexOf(highPlayer);
const highPlayerDate = highPlayer.date;
console.log({ highPlayer, highPlayerIndex, highPlayerName, highPlayerDate });

关于javascript - 查找数组中最近的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54701036/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com