簡體   English   中英

按不同的鍵對一組對象進行排序

[英]Sort an array of objects by different keys

希望你能幫我解決這個問題,我很確定這很簡單,但我覺得我在這里遺漏了一些基本概念。

我有一系列的對象,如

[{
  "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!"
}]

如您所見,有些對象尚未更新,因此這些值設置為 null,但其他對象已更新,因此我想做的是按創建日期對該數組進行排序,除非它已更新,這意味着 updatedDate 是比較此數組的鍵值。

我試過了:

let comments = conversation.sort(
   (a,b) => {
      if (a.updateDate){
         return (a.creationDate - b.updateDate);
      } else {
        return (b.creationDate - a.updateDate);
      }
});

但顯然它僅在比較未更新的對象時才有效。 我很確定我遺漏了一些東西,但我不確定,我還想過將數組拆分為更新的數組和未更新的數組,然后合並它,但這對我來說聽起來有點麻煩。

拜托了,如果你能給我一個提示,那就太好了!

非常感謝!

您可以使用邏輯 OR || 並作為默認的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; }

這樣就可以通過選擇為每個對象定義的日期來使用定義的日期。

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

首先評估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)

 ((a.updateDate || 0) - (b.updateDate || 0)) || a.creationDate - b.creationDate

如果兩個 updateDates 都沒有設置,則按創建日期進行比較,否則 updateSet 首先出現。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM