ES6 - 数组方法 forEach()

forEach() 方法为数组中的每个元素调用一个函数。

语法

array.forEach(callback[, thisObject]);

参数

  • callback − 用于测试每个元素的函数。

  • thisObject − 执行回调时用作 this 的对象。

返回值

返回创建的数组。

示例:forEach()

var nums = new Array(12,13,14,15)  
console.log("打印原始数组......")

nums.forEach(function(val,index) {
    console.log(val)
})
nums.reverse() //反转数组元素
console.log("打印反转数组....")

nums.forEach(function(val,index){
    console.log(val)
})

输出

打印原始数组....
12
13
14
15
打印反转数组...
15 
14 
13 
1