-2
如何將JavaScript對象轉換爲對象數組?數組元素必須由index
財產的原始對象進行排序:將javascript對象轉換爲具有特定順序的數組
輸入:
var o = {
"0gfbpa1508024708952": {
title: "hi daughter",
index: 3
},
"b8tl5k1508024741100": {
title: "hi dad",
index: 0
},
"cxr9x1508024697320": {
title: "hi son",
index: 2
},
"qua2802469732": {
title: "hi cousin",
index: 9007199254740991
},
"tck80i1508024731561": {
title: "hi mom",
index: 1
}
};
輸出:
[
{id: "b8tl5k1508024741100", title: "hi dad"}, // array index 0
{id: "tck80i1508024731561", title: "hi mom"}, // array index 1
{id: "cxr9x1508024697320", title: "hi son"}, // array index 2
{id: "0gfbpa1508024708952", title: "hi daughter"}, // array index 3
{id: "qua2802469732", title: "hi cousin"} // array index 4
]
This question只與訂貨有助於/的排序陣列。它沒有解釋如何(1)將對象鍵轉換爲數組對象id
屬性,以及(2)如何從數組中刪除index
鍵。這裏正在進行轉型,而不僅僅是基於對象鍵的排序/排序。
[轉換對象文本排序後的數組]的可能的複製(https://stackoverflow.com/questions/13694883/converting-an-object-literal-to-a-sorted-array) –