- var arr = ["a", "b", "c", "d", "e"];
- var obj = { a: 'one', b: 'two', c: 'three', d: 'four', e: 'five' };
- $.each(obj,function(key,value){
- console.log("Obj :" + key + '-' + value);
- })
- $.each(arr,function(key,value){
- console.log("arr :" + key + '-' + value);
- })
-
输出结果:
Obj :a-one
Obj :b-two
Obj :c-three
Obj :d-four
Obj :e-five
arr :0-a
arr :1-b
arr :2-c
arr :3-d
arr :4-e -
js中遍历Map对象
-
for(var key in jsonData)
console.log("属性:" + key + ",值:"+ jsonData[key]);
}
遍历map
var testMap={"key1":"value1","key2":"value2","key3":new Array("one","two","three")};
for(var key in testMap){
alert(“testMap[”+key+"]="+testMap[key]);
}
遍历数组
//1. 普通的循环遍历方式
var arr = [11,22,33,55];
for(var i= 0;i<arr.length;i++){
console.log("第一种遍历方式\t"+arr[i]);
}
//2、for ..in 遍历方式
for(var index in arr){
console.log("第二种遍历方式\t"+arr[index]);
}