zoukankan      html  css  js  c++  java
  • 对数组对象根据时间倒序排列

    在接口中查询出来的数据是这样的:

    "contents": [
    {
    "itemContent": "问题提出",
    "createdOn": 2017-01-6,
    "itemType": 0,
    "imgs": []
    },
    {
    "itemContent": "回答",
    "createdOn": 2017-01-12,
    "itemType": 2,
    "imgs": []
    },
    {
    "itemContent": "小毕",
    "createdOn": 2017-01-12,
    "itemType": 2,
    "imgs": []
    },
    {
    "itemContent": "456456465",
    "createdOn": 2017-01-13,
    "itemType": 2,
    "imgs": []
    },
    {
    "itemContent": "礼拜四",
    "createdOn": 2017-01-13,
    "itemType": 2,
    "imgs": []
    }
    ]
    

    从数据中可以看出,查出来的数据是由时间的升序排序的,有什么方法让这个数组中的对象按时间倒序排列?我想要的结果是这样的:

    "contents": [
    {
    "itemContent": "礼拜四",
    "createdOn": 2017-01-13,
    "itemType": 2,
    "imgs": []
    },
    {
    "itemContent": "456456465",
    "createdOn": 2017-01-13,
    "itemType": 2,
    "imgs": []
    },
    {
    "itemContent": "小毕",
    "createdOn": 2017-01-12,
    "itemType": 2,
    "imgs": []
    },
    {
    "itemContent": "回答",
    "createdOn": 2017-01-12,
    "itemType": 2,
    "imgs": []
    },
    {
    "itemContent": "问题提出",
    "createdOn": 2017-01-6,
    "itemType": 0,
    "imgs": []
    }
    ]

    使用reverse() 颠倒数组中元素的顺序,这时,arr中的对象已经颠倒过来了,比排序还要简单

    var arr = contents.reverse();
  • 相关阅读:
    耐心
    百度年会
    聊聊今年的春节联欢晚会
    网速调查
    热烈欢迎两位园友加盟
    遥远的路
    博客园博客程序架构设计图初稿
    [公告]社区与博客实现了登录整合
    博客园上海俱乐部第二次活动继续报道
    [收藏]《观察与思考》:相信中国,寻找下一个比尔·盖茨
  • 原文地址:https://www.cnblogs.com/baiyygynui/p/6278339.html
Copyright © 2011-2022 走看看