早教吧 育儿知识 作业答案 考试题库 百科 知识分享

json按照值的大小排序并输出键,数据如下varjson={a:8.167,b:1.492,c:2.782,d:4.253,e:12.702,f:2.228,g:2.015,h:6.094,i:6.966,j:0.153,k:0.772,l:4.025,m:2.406,n:6.749,o:7.507,p:1.929,q:0.095,r:5.987,s:6.327,t:9.056,u:2.758,v:0.978,w:2.

题目详情
json 按照值的大小排序并输出键,数据如下
var json ={
a:8.167,b:1.492,c:2.782,d:4.253,e:12.702,f:2.228,g:2.015,h:6.094,i:6.966,j:0.153,k:0.772,l:4.025,m:2.406,
n:6.749,o:7.507,p:1.929,q:0.095,r:5.987,s:6.327,t:9.056,u:2.758,v:0.978,w:2.360,x:0.150,y:1.974,z:0.074
}
结果类似于“s,t,a.......”
▼优质解答
答案和解析
var obj = {};
for(var key in json){
obj[json[key]] = key;
}
console.log(obj);
var arr = [];
for(var key in obj){
arr.push(key);
}
arr = arr.sort();
console.log(arr);
var obj2 = {};
for(var i = 0;i obj2[arr[i]] = obj[arr[i]]
}
console.log(obj2);