按不同的键排列对象数组

By simon at 2018-02-07 • 0人收藏 • 24人看过

希望你能帮我解决这个问题,我很确定 简单,但我觉得我错过了这里的一些基本概念。 我有一个对象的数组

[{
  "id":"123",
  "creationUser":"user1",
  "updateUser":null,
  "creationDate":1517495569000,
  "updateDate":null,
  "text":"Hello World"
},

{
  "id":"543",
  "creationUser":"user2",
  "updateUser":"user3",
  "creationDate":1517912985769,
  "updateDate":1517921704448,
  "text":"Hello people"
},

{
  "id":"847",
  "creationUser":"user 4",
  "updateUser":null,
  "creationDate":null,
  "updateDate":1517913015110,
  "text":"Text 1"
},

{
  "id":"344",
  "creationUser":"user 1",
  "updateUser":"central",
  "creationDate":1517912979283,
  "updateDate":1517923926834,
  "text":"Aloha!"
}]
正如你所看到的,有些对象并没有被更新 VALUes被设置为null,但其他人已被更新,所以我想 做的是按创建日期排序数组,除非它是h作为更新,哪个 意思是updatedDate是比较这个数组的关键值。 我努力了:
let comments = conversation.sort(
   (a,b) => {
      if (a.updateDate){
         return (a.creationDate - b.updateDate);
      } else {
        return (b.creationDate - a.updateDate);
      }
});
但是很明显只有在比较非更新的对象时才起作用。我很漂亮 当然,我错过了一些东西,但我不确定,我也想过点击 数组到更新的数组和非更新数组,然后merege它,但它 听起来有点哈克给我。 请拜托,我你可以给我一个提示,这将是伟大的! 非常感谢!

5 个回复 | 最后更新于 2018-02-07
2018-02-07   #1

你可以使用[logical OR ||](https://developer.mozilla.org/en- 美国/文档/网络/的JavaScript /参考/运营/ LogicalOpe猛龙#逻辑或\(\)) 并作为默认creationDate。

var array = [{ id: "123", creationUser: "user1", updateUser: null, creationDate: 1517495569000, updateDate: null, text: "Hello World" }, { id: "543", creationUser: "user2", updateUser: "user3", creationDate: 1517912985769, updateDate: 1517921704448, text: "Hello people" }, { id: "847", creationUser: "user 4", updateUser: null, creationDate: null, updateDate: 1517913015110, text: "Text 1" }, { id: "344", creationUser: "user 1", updateUser: "central", creationDate: 1517912979283, updateDate: 1517923926834, text: "Aloha!" }];

array.sort((a, b) => (a.updateDate || a.creationDate) - (b.updateDate || b.creationDate));

console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }

2018-02-07   #2

你可以使用[logical OR ||](https://developer.mozilla.org/en- 美国/文档/网络/的JavaScript /参考/运营/ LogicalOpe猛龙#逻辑或\(\)) 并作为默认creationDate。

var array = [{ id: "123", creationUser: "user1", updateUser: null, creationDate: 1517495569000, updateDate: null, text: "Hello World" }, { id: "543", creationUser: "user2", updateUser: "user3", creationDate: 1517912985769, updateDate: 1517921704448, text: "Hello people" }, { id: "847", creationUser: "user 4", updateUser: null, creationDate: null, updateDate: 1517913015110, text: "Text 1" }, { id: "344", creationUser: "user 1", updateUser: "central", creationDate: 1517912979283, updateDate: 1517923926834, text: "Aloha!" }];

array.sort((a, b) => (a.updateDate || a.creationDate) - (b.updateDate || b.creationDate));

console.log(array);
.as-console-wrapper { max-height: 100% !important; top: 0; }

2018-02-07   #3

所以这样做,所以你通过选择一个是使用定义的日期 为每个对象定义。

  var aDate = a.updateDate || a.creationDate
  var bDate = b.updateDate || b.creationDate
  return bDate - aDate

2018-02-07   #4

首先评估updateDate属性的存在,如果不存在, 使用creationDate属性:

var data =[{
  "id":"123",
  "creationUser":"user1",
  "updateUser":null,
  "creationDate":1517495569000,
  "updateDate":null,
  "text":"Hello World"
},

{
  "id":"543",
  "creationUser":"user2",
  "updateUser":"user3",
  "creationDate":1517912985769,
  "updateDate":1517921704448,
  "text":"Hello people"
},

{
  "id":"847",
  "creationUser":"user 4",
  "updateUser":null,
  "creationDate":null,
  "updateDate":1517913015110,
  "text":"Text 1"
},

{
  "id":"344",
  "creationUser":"user 1",
  "updateUser":"central",
  "creationDate":1517912979283,
  "updateDate":1517923926834,
  "text":"Aloha!"
}];

data.sort(function (a,b) {
   var aDate = a.updateDate?a.updateDate:a.creationDate;
   var bDate = b.updateDate?b.updateDate:b.creationDate;
   return aDate - bDate;
});
console.log(data)

2018-02-07   #5

 ((a.updateDate || 0) - (b.updateDate || 0)) || a.creationDate - b.creationDate
如果两个updateDates均未设置,则按创建日期进行比较,否则为 updateSet先来。

登录后方可回帖

Loading...