JS map对象顺序调整

新手上路,请多包涵

问题描述

clipboard.png 想把这个按照7月19日-7月16日反过来排序。请教如何处理呢?

问题出现的环境背景及自己尝试过哪些方法

相关代码

for (let i = 0; i < rs.length; i++) {
    let timeKey = moment(rs[i].timestamp).format("YYYY/MM/DD");
    if (this.direction === 1) {
        if ((timeKey in curDatas) === true) {
            curDatas[timeKey].unshift(rs[i])
            console.log(curDatas[timeKey]);
        } else {
            curDatas[timeKey] = [];
            curDatas[timeKey].unshift(rs[i])
        }
    } else {
        if ((timeKey in curDatas) === true) {
            curDatas[timeKey].push(rs[i])
        } else {
            curDatas[timeKey] = [];
            curDatas[timeKey].push(rs[i])
        }
    }
}

你期待的结果是什么?实际看到的错误信息又是什么?

direction代表向上向下的方向,所以有一个判断。
想让最后的curDatas 按照20190719-20190718-201190716 的顺序排列该如何操作?

阅读 8.2k
2 个回答

只是倒序过来吧?

var data = Object.keys(obj).reverse().reduce(function(a,b){ a[b]=obj[b]; return a;  },{});

JS Object 里的属性的位置是不确定的...想要实现排序可以转换成 Array,像这样的 [{'2017':{}},{'2018':{}},{'2019':{}}]

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题