在js中如何循环这样的数据
1-后台生成的数据是result = {"1":"A","2":"B","3":"C","4":"D"};
2-在js中是如何循环上面的结果呢,既要key值也value值。
会的朋友帮忙看看。
------解决方案--------------------普通版
for(var key in result ){
var value= result [key];
}
//each版,先定义each
var xx= {},
xx.each= function(object,fn, bind){
for (var key in object){
if (hasOwnProperty.call(object, key)) fn.call(bind, object[key], key, object);
}
}
//然后实际使用时就可以
xx.each(result,function(value,key){
alert(key+".value="+ value);
});
------解决方案--------------------<script type="text/javascript">
var result = {"1":"A","2":"B","3":"C","4":"D"};
for(var x in result){
alert(x+': '+result[x])
}
</script>