首页 > 编程语言 > 详细

遍历数组

时间:2020-04-12 17:11:24      阅读:54      评论:0      收藏:0      [点我收藏+]
    var arr = [3,5,,7,8,,,4] // 稀疏数组
    // for循环遍历数组遇到空元素会输出undefined
    for (var i = 0; i < arr.length; i++) {
      console.log(i, arr[i])
    }

    console.log(‘----------‘)

    // for...in在遍历数组的时候会自动跳过空元素
    for (var index in arr) {
      // index是数组的索引,他是字符串类型,但是并不影响取数据
      console.log(index, arr[index])
    }

    console.log(‘-------‘)
    // 遍历数组的第三种方式:for...of
    // 直接取值,不取下标,稀疏数组中的undefined也会被遍历出来
    for (var value of arr) {
      console.log(value)
    }

 

遍历数组

原文:https://www.cnblogs.com/strongerPian/p/12682720.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!