js中遍历

来源:互联网 发布:linux解压软件下载 编辑:程序博客网 时间:2024/06/07 00:04
[html] view plain cop 
  1. var arr = ["a", "b", "c", "d", "e"];    
  2. var obj = { a: 'one', b: 'two', c: 'three', d: 'four', e: 'five' };  
  3.   
  4. $.each(obj,function(key,value){  
  5.     console.log("Obj :" + key + '-' + value);  
  6. })  
  7.   
  8.   
  9. $.each(arr,function(key,value){  
  10.     console.log("arr :" + key + '-' + value);  
  11. })  
  12. 输出结果:

    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

  13.  

    js中遍历Map对象


  14. 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]);  

    }