javascript - jQuery for循环出来的数据,根据时间的先后,依次把DOM节点放在页面上?
巴扎黑
巴扎黑 2017-04-11 11:24:52
[JavaScript讨论组]

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

"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": []
}
]
巴扎黑
巴扎黑

全部回复(4)
大家讲道理

var arr = contents.reverse();

巴扎黑
for(var i = contents.length-1 ; i >= 0; i--){
    //todo
}

可能理解有偏差
补充:
数组 reverse之后 依然会循环遍历一遍.
所以 我的意思是todo那里循环生成html, 循环结束后append到页面中

巴扎黑
  var 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.reverse();
PHPz

和vison的解决方法一样的,从数组最后一个元素开始遍历数组(然后分别一次压入到一个新的数组内,他的代码没有写全,算是补充吧)

var newContents=[];
for (var i = contents.length-1 ; i >= 0; i--){
    newContents.push(contents[i]);
}
console.log(newContents);
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号