距开课0天0时0分-10245118秒
代言2017-02-20 10:30:54 0 1 467
[PHP讨论组]举报回复话题 ↕
有一组json数据在进行for循环插入到dom时,
var tt = data.serach_house_price;//json数据 var html3 = ''; for(var i in tt) { html3 +='<li data-sorce = '+i+'>'+tt[i]+'</li>'; }
输出结果如图所示,最后一个key循环到第一个位置了,如何对它们按大小进行重新排序呢?
1
0
分享
大家讲道理 2017-02-20 10:31:261楼
var tt = { '0-40': '40万以下', '40-60': '40-60万', '60-80': '60-80万', '80-100': '80-100万', '100-150': '100-150万', '150-200': '150-200万', '200-300': '200-300万', '300-400': '300-400万', '400': '400万以上', } var sortKeys = Object.keys(tt).sort(function(i1,i2){ return i2.split('-')[0] - i1.split('-')[0] }); var html3 = ''; for(var i=0, len=sortKeys.length; i<len; i++) { html3 +='<li data-sorce = '+sortKeys[i]+'>'+tt[sortKeys[i]]+'</li>\n'; } console.log(html3);
赞 +0添加回复
大家讲道理 2017-02-20 10:31:261楼
赞 +0添加回复