为什么遍历json的时,当key为0,1,2,3之类的数字的时候会自动按照key的大小排序?
例如:json={"1":"a","3":"c","2":"b"};遍历:$.each(json,function(i){console.log("key="+i+",v...
例如:
json={"1":"a","3":"c","2":"b"};
遍历:
$.each(json, function(i) {
console.log("key = " + i+",val="+json[i]);
});
或
for (var key in city1){
console.log("key = " + i+",val="+json[i]);
}
得到的却是
key = 1,val= a
key = 2,val= b
key = 3,val= c
按理说应该是
key = 1,val= a
key = 3,val= c
key = 2,val= b
而如果把key变为01,03,02则显示正常
求各位解惑
额,“或” 字后面的代码是
for (var key in json){
console.log("key = " + i+",val="+json[i]);
} 展开
json={"1":"a","3":"c","2":"b"};
遍历:
$.each(json, function(i) {
console.log("key = " + i+",val="+json[i]);
});
或
for (var key in city1){
console.log("key = " + i+",val="+json[i]);
}
得到的却是
key = 1,val= a
key = 2,val= b
key = 3,val= c
按理说应该是
key = 1,val= a
key = 3,val= c
key = 2,val= b
而如果把key变为01,03,02则显示正常
求各位解惑
额,“或” 字后面的代码是
for (var key in json){
console.log("key = " + i+",val="+json[i]);
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询