2016-01-21 77 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>

相关问题