2
我有一个JavaScript对象,看起来像如下基于日期键排序JavaScript对象
testObj = {
1/10/2015: {},
2/10/2015: {},
3/10/2015: {},
4/10/2015: {},
29/09/2015: {},
30/09/2015: {}
}
现在,我想解决这使得日期按日期按升序排列。为此,我做了以下工作
const orderedDates = {};
Object.keys(testObj).sort(function(a, b) {
return moment(moment(b, 'DD/MM/YYYY') - moment(a, 'DD/MM/YYYY')).format('DD/MM/YYYY');
}).forEach(function(key) {
orderedDates[key] = testObj[key];
})
rangeObj = orderedDates;
但是,这并不排序日期。它仍然返回与testObj
相同的确切对象。如何根据日期键对对象进行排序?
这里的答案http://stackoverflow.com/questions/5525795/does-javascript-guarantee-object-property-order – mguimard
@mguimard但使用const'的'将指示[ES6](http://stackoverflow.com/questions/30076219/does-es6-introduce-a-well-defined-order-of-enumeration-for-object-properties),而不是ES5 – CodingIntrigue