Welcome to my website, have a nice day!
Dream it, Do it, Make it!

JS中forEach()用法

forEach() 方法对数组的每个元素执行一次提供的函数。

示例1:

var array1 = ['a', 'b', 'c'];

array1.forEach(function(element) {
  console.log(element);
});

// expected output: "a"
// expected output: "b"
// expected output: "c"

示例2:

var array = ['a', 'b', 'c'];
array.forEach(function(currentValue,index,arr){
 console.log(currentValue);
});

//a
//b
//c

语法:

arr.forEach(callback[, thisArg]);

参数:

callback,必选,为数组中每个元素执行的函数,该函数接收三个参数:
    currentValue
    数组中正在处理的当前元素。
    index可选
    数组中正在处理的当前元素的索引。
    array可选
    forEach() 方法正在操作的数组。
thisArg,可选参数。当执行回调函数时用作 this 的值(参考对象)。

参考:

  1. Array​.prototype​.for​Each()
赞(0)
未经允许禁止转载:Ddmit » JS中forEach()用法

评论 抢沙发

登录

找回密码

注册