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

标签 javascript html arrays function date

我试图在具有不同日期的对象数组中找到最高日期,这些日期将归因于每个对象。只要日期在 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/

相关文章:

javascript - 在页面 php 中加载文章

javascript - 如何在用户端只运行一次某个脚本?

java - 使用递归根据用户输入确定斐波那契数

c++ - 从文本文件中读取,然后在程序中使用输入

javascript - 在jsp中运行javascript函数

javascript - 巴别塔 v6 : How/Can I write a plugin that adds a new syntax (ie a new operator)?

html - 设置vue模态掩码模糊

javascript - 创建三维数组并在 Javascript 中为其赋值

javascript - 在 HTML 中用一条线连接 2 个图像。 Django 模板

javascript - Angular js 验证函数在验证的 api 调用完成之前返回