javascript - 对单个数组中的两种类型的对象进行排序

标签 javascript sorting

我有一个包含 meeting 对象的数组,该对象具有属性 utc_start。 我还有一个包含 reminder 对象的数组,该对象具有属性 utc_datetime

将它们合并后:

    combined = [];
    combined = combined.concat(meetings);
    combined = combined.concat(reminders);

是否可以对它们进行排序?每个都使用不同的属性?

combined.sort((a, b) => {

});

谢谢。

最佳答案

你可以试试:

combined.sort((a, b) => {
  const utcA = a.utc_start || a.utc_datetime;
  const utcB = b.utc_start || b.utc_datetime;

  // compare utcA with utcB
});

关于javascript - 对单个数组中的两种类型的对象进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50832771/

相关文章:

javascript - 这两行是什么意思 : o[x] = o[x] || {}; o = o[x];

javascript - 从函数返回 promise

javascript - Discord.js 机器人赠品命令 : embedSent. Reactions.get 不是函数

python - 使用自定义比较功能对列表列表进行排序

r - 如何按所有列对矩阵/数据框进行排序

javascript - 用JavaScript排序:返回 bool 值是否足以用作比较函数?

javascript - Javascript如何存储数组

Javascript 替换 innerText 而不是它的属性

c++ - 运行时检查失败 #02,随机数添加到数组

sorting - 为什么 STL unordered_map 和 unordered_set 不能通过 STL 算法排序?