• js中遍历对象的属性和值的方法


    鉴于循环目标是个对象,length是为undefined,用map等对数组的循环方法不行,对象就用此下方法
      for(var key in _this.lists.medicines){
        medicineName +=_this.lists.medicines[key].medicine.medicinePublic.medicineName + ",";
      }
     
    1
    2
    3
    4
    5
    6
    7
    8
    9
    var Statistics_Website_logo ={
                 'Website_logo_title':'学而思',
                 'Website_logo_Theme':'教育行业',
                 'Website_logo_Company':'好未来'
       };
     
    for (var Key in Statistics_Website_logo){
          Websitelogo =Websitelogo+'&'+''+Key+'='+Statistics_Website_logo[Key]+'';
        }

    JS数组遍历:

    1,普通for循环,经常用的数组遍历

    var arr = [1,2,0,3,9];
     for ( var i = 0; i <arr.length; i++){
        console.log(arr[i]);
    }

    2,优化版for循环:使用变量,将长度缓存起来,避免重复获取长度,数组很大时优化效果明显

    for(var j = 0,len = arr.length; j < len; j++){
        console.log(arr[j]);
    }

    3,forEach,ES5推出的,数组自带的循环,主要功能是遍历数组,实际性能比for还弱

    arr.forEach(function(value,i){
      console.log('forEach遍历:'+i+'--'+value);

    })

    forEach这种方法也有一个小缺陷:你不能使用break语句中断循环,也不能使用return语句返回到外层函数。

    4,map遍历,map即是 “映射”的意思 用法与 forEach 相似

    arr.map(function(value,index){
        console.log('map遍历:'+index+'--'+value);
    });

    map遍历支持使用return语句,支持return返回值

    var temp=arr.map(function(val,index){
      console.log(val);  
      return val*val           
    })
    console.log(temp);  

    forEach、map都是ECMA5新增数组的方法,所以ie9以下的浏览器还不支持

    5,for-of遍历 是ES6新增功能

    for( let i of arr){
        console.log(i);
    }
    • for-of这个方法避开了for-in循环的所有缺陷
    • 与forEach()不同的是,它可以正确响应break、continue和return语句 

    for-of循环不仅支持数组,还支持大多数类数组对象,例如DOM NodeList对象

    for-of循环也支持字符串遍历

    JS对象遍历:

    1,for-in遍历

    for-in是为遍历对象而设计的,不适用于遍历数组。

    遍历数组的缺点:数组的下标index值是数字,for-in遍历的index值"0","1","2"等是字符串

    for (var index in arr){
        console.log(arr[index]);
        console.log(index);
    }
  • 相关阅读:
    OpenJTAG与Jlink/Jlink的区别
    Ubunut 10.04 upgrade git
    HEVC播放器出炉,迅雷看看支持H.265
    平铺文理+拉伸按钮图片
    黑马程序员java基础学习IO流4
    Excel 2010实战技巧精粹
    昆明虚假楼盘吸引 2 千多人购买诈骗近 3 亿
    Canonical 公司预计最晚明年 4 月推出 Ubuntu 中国版
    Canonical 公司预计最晚明年 4 月推出 Ubuntu 中国版
    十二个理由让你不得不期待 Ubuntu10.10
  • 原文地址:https://www.cnblogs.com/Tohold/p/9039686.html
Copyright © 2020-2023  润新知