簡體   English   中英

object 已排序,但未使用 lodash sortBy 保留其鍵

[英]object is sorted but its keys are not preserved using lodash sortBy

語境

需要使用 lodash 通過readingDate對以下對象進行排序,我可以對其進行排序,但它的索引沒有保留。

原裝 JSON Object

{
    "Inventory Creation": [
      {
        "id": 150,
        "reading": "12345",
        "readingDate": "2020-07-14"
      }
    ],
    "Inventory & Check-in": [
      {
        "id": 151,
        "reading": "12345",
        "readingDate": "2020-11-14"
      }
    ],
    "Check-in": [
      {
        "id": 152,
        "reading": "12345",
        "readingDate": "2020-08-14"
      }
    ]
}

我試過的代碼

_.sortBy(unsorted, o => o[0].readingDate).reverse();

這里unsorted包含上述原始 JSON object

實際結果(我得到的)

{
    0: [
      {
        "id": 151,
        "reading": "12345",
        "readingDate": "2020-11-14"
      }
    ],
    1: [
      {
        "id": 152,
        "reading": "12345",
        "readingDate": "2020-08-14"
      }
    ],
    2: [
      {
        "id": 150,
        "reading": "12345",
        "readingDate": "2020-07-14"
      }
    ]
}

預期結果(我想要的)

{
    "Inventory & Check-in": [
      {
        "id": 151,
        "reading": "12345",
        "readingDate": "2020-11-14"
      }
    ],
    "Check-in": [
      {
        "id": 152,
        "reading": "12345",
        "readingDate": "2020-08-14"
      }
    ],
    "Inventory Creation": [
      {
        "id": 150,
        "reading": "12345",
        "readingDate": "2020-07-14"
      }
    ]
}

以下是步驟:

  • 將 object 轉換為鍵值對列表( _.toPairs()或內置的Object.entries()
  • 排序列表
  • 將鍵值對列表轉換回 object ( _.fromPairs()或內置的Object.fromEntries() )

下面的片段可以幫助你。 我使用_.chain()以獲得更好的可讀性

 const data = { "Inventory Creation": [ { id: 150, reading: "12345", readingDate: "2020-07-14", }, ], "Inventory & Check-in": [ { id: 151, reading: "12345", readingDate: "2020-11-14", }, ], "Check-in": [ { id: 152, reading: "12345", readingDate: "2020-08-14", }, ], } const res = _.chain(data).toPairs().sortBy((p) => p[1][0].readingDate).reverse().fromPairs().value() console.log(res)
 <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.19/lodash.min.js"></script>

這是集合/數組的組合。 查看您正在使用的 lodash function 及其用途。 這不起作用,您正在嘗試使用 Object 的數組函數。 function 做的正是它應該做的,對數組進行排序。 但是沒有辦法以這種方式創建排序的 object,它(意味着 lodash 函數)甚至不知道 object。

話雖如此,這是一個可行的解決方案(一般看一下 map/reduce):

const entries = Object.entries(unsortedArr);
const sorted = entries.sort((a,b) => a[1][0].readingDate < b[1][0].readingDate ? 1 : -1);
const reduced = sorted.reduce(
  (acc, obj) => {
    acc[obj[0]] = obj[1]
return acc},{});

Ofc 你可以鏈接這些調用,但我試圖讓它更容易理解。
我只是偶然發現了groupBy也可以使用。

這是一個蠻力解決方案。 首先對數組進行排序並存儲鍵。 然后使用 sortedArray 中的鍵形成數組。

var sortedArray = _.sortBy(unsortedArray, function(val, key){
    val[0].key = key;
    return val[0].readingDate;
}).reverse();

var sortedArrayWithIndex = {};

_.each(sortedArray, function(val){
    var key = val[0].key;
    delete val[0].key;
    sortedArrayWithIndex[key] = val;
});
console.log(sortedArrayWithIndex);

暫無
暫無

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

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