- forEach()
- map()----更新数组
- filter()、includes()、find()、findIndex()----筛选(删除)数组
- some()、every()----判断数组
- reduce()----叠加数组
一、arr.forEach()
遍历数组全部元素,利用回调函数对数组进行操作,自动遍历数组.length次,且无法break中途跳出循环,不可控
不支持return操作输出,return只用于控制循环是否跳出当前循环
示例:
[1,2,3,4].forEach((n)=>{
console.log(n);
})
二、arr.map()----更新数组
- 创建新数组
- 不改变原数组
- 输出的是 return什么就输出什么 新数组
- 回调函数参数,item(数组元素)、index(序列)、arr(数组本身)
- 使用return操作输出,会循环数组每一项,并在回调函数中操作
示例:
var arr = [1,2,3,4,5] ;
var newArr = arr.map(function(item,index){
return item*2 ; //操作更新数组
})
console.log(newArr); //打印新数组
console.log(arr); //打印原数组,map()没有改变原数组