如何用js实现json数据key值的排序问题

有一组json数据在进行for循环插入到dom时,

https://img1.mukewang.com/5bf0ffcb0001c64b03110313.jpg

var tt = data.serach_house_price;//json数据

var html3 = '';

for(var i in tt) {

        html3 +='<li data-sorce = '+i+'>'+tt[i]+'</li>';

}

输出结果如图所示,最后一个key循环到第一个位置了,如何对它们按大小进行重新排序呢?

https://img4.mukewang.com/5bf0ffd800015cc401880254.jpg

牧羊人nacy
浏览 590回答 1
1回答

万千封印

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){&nbsp; return i2.split('-')[0] - i1.split('-')[0]&nbsp;});var html3 = '';for(var i=0, len=sortKeys.length; i<len; i++) {&nbsp; &nbsp; &nbsp; &nbsp; html3 +='<li data-sorce = '+sortKeys[i]+'>'+tt[sortKeys[i]]+'</li>\n';}console.log(html3);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript