有一组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 回答

万千封印
TA贡献1891条经验 获得超3个赞
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/150
提交
取消