Advertisement

遍历数组的方法

阅读量:

示例中所展示的数组为:

复制代码
    var arr = [
      { id: 1, name: '王昭君' },
      { id: 2, name: '安琪拉' },
      { id: 3, name: '鲁班七号' },
    ];
    var nums = [1, 2, 3, 4, 5];
    
    
      
      
      
      
      
      
    

1. 普通for循环;

复制代码
    for (let i = 0; i < arr.length; i++) {
      console.log(arr[i], i);
    }
    
    
      
      
      
    

2,forEach循环;

复制代码
    arr.forEach((ele, index) => {
      console.log(ele, index);
    });
    
    
      
      
      
    

3、for…in,(主要是用来遍历对象,不建议遍历数组)

![](https://ad.itadn.com/c/weblog/blog-img/images/

全部评论 (0)

还没有任何评论哟~