2016-01-21 45 views
0

爲了我們的前端應用程序的目的,我需要攝取一組對象(比如約20),然後將數組轉換爲鍵/值對象。爲什麼這會失去排序?

data = [ 
    {name: 'first', uid: 789, start: '2016-01-20 08:00:00'}, 
    {name: 'second', uid: 492, start: '2016-01-20 15:00:00'}, 
    {name: 'third', uid: 324, start: '2016-01-20 10:00:00'}, 
    {name: 'fourth', uid: 923, start: '2016-01-20 14:30:00'}, 
    // ... 
]; 

然後我通過此有序陣列上運行此start

data.sort(function (a, b) { 
    var aStart = new Date(a.start), 
     bStart = new Date(b.start); 

    if (aStart < bStart) return -1; 
    if (aStart > bStart) return 1; 
    return 0; 
}); 

排序然後,爲快速訪問基於UID數據的目的,我循環的將其轉換爲K /對象v:

var stored = {}; 
for (var i = 0; i < data.length; i++) { 
    stored[data[i].uid] = data[i]; 
} 

這讓我通過data每次我需要一個給定的鄰位時間做這樣的事情stored[uid]而不必循環bject。

問題

在通過循環和創造stored對象,我似乎失去排序順序。

排序後:

2016-01-20 08:00:00 // 789 
2016-01-20 10:00:00 // 324 
2016-01-20 14:30:00 // 923 
2016-01-20 15:00:00 // 492 

轉換爲對象

Object.keys(sorted).map(function (id, index) { 
    console.log(sorted[id].start) 
}); 

產量的影響:

2016-01-20 08:00:00 // 789 
2016-01-20 14:30:00 // 923 
2016-01-20 15:00:00 // 492 
2016-01-20 10:00:00 // 324 

正如你可以看到,上午10時事件(324)現在是在名單的結尾,我不確定爲什麼發生這種情況。

+3

JavaScript對象不能保證維持插入的順序。 – thefourtheye

+0

不真實,我能想到的每個實現都維護插入順序。你能發佈一個更完整的代碼示例嗎? –

+0

這就是全部。所涉及的攝入數據沒有其他操作。考慮到@fourtheye的評論,我可以通過首先在渲染時轉換回數組並在那裏排序來修復排序順序。 – adamell

回答

1

正如這篇文章所說,雖然有些引擎確實在對象中保留了插入順序,但這不是必需的。有兩種方法來保持屬性的順序:

  • 保留一個排序的鍵數組;迭代該數組以依次訪問對象屬性。

var data = [ 
 
    {name: 'first', uid: 789, start: '2016-01-20 08:00:00'}, 
 
    {name: 'second', uid: 492, start: '2016-01-20 15:00:00'}, 
 
    {name: 'third', uid: 324, start: '2016-01-20 10:00:00'}, 
 
    {name: 'fourth', uid: 923, start: '2016-01-20 14:30:00'} 
 
]; 
 

 
var stored = {}; 
 
for (var i = 0; i < data.length; i++) { 
 
    stored[data[i].uid] = data[i]; 
 
} 
 

 
var keys = Object.keys(stored); 
 
keys.sort(function(a, b) { 
 
    var aStart = new Date(stored[a].start), 
 
    var bStart = new Date(stored[b].start); 
 

 
    if (aStart < bStart) return -1; 
 
    if (aStart > bStart) return 1; 
 
    return 0; 
 
}); 
 

 
keys.forEach(function(key) { 
 
    console.log(stored[key].start); 
 
});
<!-- results pane console output; see http://meta.stackexchange.com/a/242491 --> 
 
<script src="http://gh-canon.github.io/stack-snippet-console/console.min.js"></script>

  • 使用ES6 Map,這是保證,以保持插入順序。

var data = [ 
 
    {name: 'first', uid: 789, start: '2016-01-20 08:00:00'}, 
 
    {name: 'second', uid: 492, start: '2016-01-20 15:00:00'}, 
 
    {name: 'third', uid: 324, start: '2016-01-20 10:00:00'}, 
 
    {name: 'fourth', uid: 923, start: '2016-01-20 14:30:00'} 
 
]; 
 

 
data.sort(function(a, b) { 
 
    var aStart = new Date(a.start), 
 
    bStart = new Date(b.start); 
 

 
    if (aStart < bStart) return -1; 
 
    if (aStart > bStart) return 1; 
 
    return 0; 
 
}); 
 

 
var stored = new Map(); 
 
for (var i = 0; i < data.length; i++) { 
 
    stored.set(data[i].uid, data[i]); 
 
} 
 

 
stored.forEach(function(value) { 
 
    console.log(value.start); 
 
});
<!-- results pane console output; see http://meta.stackexchange.com/a/242491 --> 
 
<script src="http://gh-canon.github.io/stack-snippet-console/console.min.js"></script>

相關問題