【问题标题】:Issue with a sorting object array by date按日期排序对象数组的问题
【发布时间】:2020-10-27 22:23:15
【问题描述】:

编辑: 找到了问题的解决方案,我认为 Date 将此日期 ("29-04-2020") 解释为 DD-MM-YYYY,而不是 MM-DD-YYYY
例如,我有一个对象数组:

let todos: object[] = [
      {
        'id': 1,
        'title': 'Test',
        'description': 'Just a new task',
        'priority': 1,
        'status': 'during',
        'date': new Date('29-05-2020')
      },
      {
        'id': 2,
        'title': 'test23',
        'description': 'Just a new task23',
        'priority': 3,
        'status': 'done',
        'date': new Date('29-05-2020')
      },
      {
        'id': 3,
        'title': 'test3',
        'description': 'Just a new task3',
        'priority': 2,
        'status': 'in process',
        'date': new Date('29-05-2020')
      },
      {
        'id': 4,
        'title': 'test3',
        'description': 'Just a new task3',
        'priority': 1,
        'status': 'during',
        'date': new Date('29-05-2020')
      },
      {
        'id': 5,
        'title': 'test3',
        'description': 'Just a new task3',
        'priority': 2,
        'status': 'done',
        'date': null
      },
      {
        'id': 6,
        'title': 'test3',
        'description': 'Just a new task3',
        'priority': 1,
        'status': 'in process',
        'date': new Date('29-05-2020')
      },
]

我的任务是:按日期对这些对象进行排序(我还需要跳过属性为 null 的键日期(这就是我有 operator if 的原因,因为我不知道如何以另一种方式做到这一点)。所以我已经创建了一个函数:

function sortByDate() {
    todos.sort((a, b) => {
        console.log(a, b)
        if (a['date'] == null) {
            return 1;
        }
        if (b['date'] == null) {
            return -1;
        }
        return a['date'].getTime() - b['date'].getTime()
    })
    return todos
}

但我在键“日期”(它有一个属性,不是空)上得到了排序的对象数组。而不是日期的良好属性(我没有 null )我在关键的“日期”中有属性:“无效日期”,我不知道如何修复它。

【问题讨论】:

  • 因为它们是对象,您必须从对象键中删除 单引号 '。见docs

标签: arrays typescript sorting


【解决方案1】:

日期格式应为yyyy-MM-dd

试试下面的代码

let varj = [
      {
        'id': 1,
        'title': 'Test',
        'description': 'Just a new task',
        'priority': 1,
        'status': 'during',
        'date': new Date('2020-05-29')
      },
      {
        'id': 2,
        'title': 'test23',
        'description': 'Just a new task23',
        'priority': 3,
        'status': 'done',
        'date': new Date('2020-05-29')
      },
      {
        'id': 3,
        'title': 'test3',
        'description': 'Just a new task3',
        'priority': 2,
        'status': 'in process',
        'date': new Date('2020-05-29')
      },
      {
        'id': 4,
        'title': 'test3',
        'description': 'Just a new task3',
        'priority': 1,
        'status': 'during',
        'date': new Date('2020-05-29')
      },
      {
        'id': 5,
        'title': 'test3',
        'description': 'Just a new task3',
        'priority': 2,
        'status': 'done',
        'date': null
      },
      {
        'id': 6,
        'title': 'test3',
        'description': 'Just a new task3',
        'priority': 1,
        'status': 'in process',
        'date': new Date('2020-04-29')
      },
];


function sortByDate(todos) {
    todos = todos.filter(obj => obj.date !== null).sort((a, b) => {
        if(a.date == b.date) return 0;
        
        if (a.date < b.date)
            return -1;
        if (a.date > b.date)
            return 1;
        return 0;
    });
    console.log(todos);
    return todos
}

【讨论】:

  • 是的,我忘记了:)。感谢您记住它。
  • @user9779830 您可以接受最佳答案。要么是我的答案,要么是另一个答案。
【解决方案2】:

我在您的代码中发现了以下问题:

  1. 对象键是字符串而不是标识符。见this
  2. 日期字符串反转。您必须从docs 之后的字符串创建日期。

您必须使用filter 删除无效日期,然后调用sort 方法。

如果您希望数组按 DESC 顺序按日期排序,请使用:

const todos_filtered_sorted_desc = todos_filtered.sort((a, b) => b.date - a.date);

否则按 ASC 顺序使用:

const todos_filtered_sorted_asc = todos_filtered.sort((a, b) => a.date - b.date);

这是工作代码(已更正 + 不同日期):

const todos = [
      {
        id: 1,
        title: 'Test',
        description: 'Just a new task',
        priority: 1,
        status: 'during',
        date: new Date('2020-05-29')
      },
      {
        id: 2,
        title: 'test23',
        description: 'Just a new task23',
        priority: 3,
        status: 'done',
        date: new Date('2019-05-29')
      },
      {
        id: 3,
        title: 'test3',
        description: 'Just a new task3',
        priority: 2,
        status: 'in process',
        date: new Date('2021-05-29')
      },
      {
        id: 4,
        title: 'test3',
        description: 'Just a new task3',
        priority: 1,
        status: 'during',
        date: new Date('2008-05-29')
      },
      {
        id: 5,
        title: 'test3',
        description: 'Just a new task3',
        priority: 2,
        status: 'done',
        date: null
      },
      {
        id: 6,
        title: 'test3',
        description: 'Just a new task3',
        priority: 1,
        status: 'in process',
        date: new Date('2000-05-29')
      },
];

console.log(`TODOS[${todos.length}]: ${JSON.stringify(todos)}`);

const todos_filtered = todos.filter(todo => todo !== null && todo !== undefined && todo.date !== null && todo.date !== undefined && todo.date instanceof Date);

console.log(`TODOS FILTERED[${todos_filtered.length}]: ${JSON.stringify(todos_filtered)}`);

const todos_filtered_sorted = todos_filtered.sort((a, b) => b.date - a.date);

console.log(`TODOS FILTERED SORTED[${todos_filtered_sorted.length}]: ${JSON.stringify(todos_filtered_sorted)}`);

您可以将所有功能链接到一个操作中:

const todos_filtered_sorted = todos
                               .filter(todo => todo !== null && todo !== undefined
                                 && todo.date !== null && todo.date !== undefined
                                 && todo.date instanceof Date)
                               .sort((a, b) => b.date - a.date);

希望对你有帮助:)

【讨论】:

  • 非常感谢,但遗憾的是这个结合过滤和排序的单一操作 - 不起作用:(,问题是 - 我不知道为什么。这是link
  • 我没有得到一个正常的日期,而是得到了无效的日期
  • 哦,对不起,我忘记了新日期适用于 MM-DD-YYYY 格式,而不是 DD-MM-YYYY。感谢您的帮助!
  • 查看我在答案中发现的问题。要创建一个有效的日期,你必须输入 YEAR-MONTH-DAY 但是你有 DAY-MONTH-YEAR 并且 Date 构造函数说你有​​一个无效的日期。
  • 是的,新的 Date 构造函数将 ("29-04-2020") 解释为 (MM-DD-YYYY) 而不是 (DD-MM-YYYY)
猜你喜欢
  • 1970-01-01
  • 2011-10-11
  • 1970-01-01
  • 2019-01-22
  • 2017-05-21
  • 1970-01-01
  • 1970-01-01
  • 2021-06-01
  • 1970-01-01
相关资源
最近更新 更多