遍历数组的方法
for循环遍历数组
JS
let arr = [10, 23, 45, 56, 89];
for(let i = 0; i < arr.length;i++){
  console.log(i,arr[i]);
}

for-in循环
JS
let arr = [10, 23, 45, 56, 89];
for(let i in arr){
    //i 是数组的下标 arr[i] 读取数组中的值
    console.log(i,arr[i]);
}

forEach( )遍历数组
说明:forEach()遍历数组 数组对象的方法 参数是一个函数,函数有俩个参数
JS
let arr = [10, 23, 45, 56, 89];
arr.forEach(function(item,index){
    // item 代表数组中的每个值,  index 代表数组的索引值
    console.log(item,index);
})

map( ) 遍历数组
说明:返回值是一个新数组; 执行的操作是把原数组中的每个值按照指定的规则映射到新数组中
JS
let newArr = arr.map(function(item,index){
    //  item 代表数组中的每个值,  index 代表数组的索引值
    console.log(item,index);
    // 指定映射规则,通过return返回映射后的值
    return item * 10;
})
console.log(newArr);

 
                     
                    
                 
                    
                
 
                
            
         
         浙公网安备 33010602011771号
浙公网安备 33010602011771号