数组遍历的方法
1.for循环遍历
2.forEach()
//匿名函数 function(){} arr.forEach(function(item,index){ //index索引下标 item数组元素 })
3.map arr.map(function(item,index){ //index索引下标 item数组元素 return 新元素
})
4.filter//过滤,找到满足条件的语句 arr.filter(function(item,index){ //index索引下标 item数组元素 return 条件
})
5.find//查找满足条件的第一个语句 arr.find(function(item,index){ //index索引下标 item数组元素 return 条件
})
6.every//查找满足条件的所有元素,所有满足true,不满足false arr.find(function(item,index){ //index索引下标 item数组元素 return 条件
})
7.some//查找满足条件的所有元素,所有有一个满足true,全不满足false arr.some(function(item,index){ //index索引下标 item数组元素 return 条件 })
8.reduce//应用累加器。数组中的每个值从左到右开始合并,最终为一个值 arr.reduce(funcation(previousValue,currentValue,currentIndex){
},initValue)
previousValue:上一个值,如果initValue存在,第一遍遍历是previousValue就是 currentValue:当前数组元素,currentIndex:当前数组下标 return 返回到previousValue,//返回成为新的默认值 }