forEach 方法 (Array) (JavaScript)

为数组中的每个元素执行指定操作。

语法

array1.forEach(callbackfn[, thisArg])

参数

 

参数

定义

array1

必选。一个数组对象。

callbackfn

必选。最多可以接受三个参数的函数。对于数组中的每个元素,forEach 都会调用 callbackfn 函数一次。

thisArg

可选。 callbackfn 函数中的 this 关键字可引用的对象。如果省略 thisArg,则 undefined 将用作 this 值。

如果 callbackfn 参数不是函数对象,则将引发 TypeError 异常。

Exception    Condition

对于数组中出现的每个元素,forEach 方法都会调用 callbackfn 函数一次(采用升序索引顺序)。将不会为数组中缺少的元素调用回调函数。

除了数组对象之外,forEach 方法可由具有 length 属性且具有已按数字编制索引的属性名的任何对象使用。

回调函数的语法如下所示:

function callbackfn(value, index, array1)

你可使用最多三个参数来声明回调函数。

回调函数的参数如下所示。

回调参数

定义

Value

数组元素的值。

index

数组元素的数字索引。

array1

包含该元素的数组对象。

forEach 方法不直接修改原始数组,但回调函数可能会修改它。下表描述了在 forEach 方法启动后修改数组对象所获得的结果。

forEach 方法启动后的条件

元素是否传递给回调函数?

在数组的原始长度之外添加元素。

否。

添加元素以填充数组中缺少的元素。

是,如果该索引尚未传递给回调函数。

元素已更改。

是,如果该元素尚未传递给回调函数。

从数组中删除元素。

否,除非该元素已传递给回调函数。

下面的示例阐释了 forEach 方法的用法。

 1 // Define the callback function.
 2 function ShowResults(value, index, ar) {
 3     document.write("value: " + value);
 4     document.write(" index: " + index);
 5     document.write("<br />");
 6 }
 7 
 8 // Create an array.
 9 var letters = ['ab', 'cd', 'ef'];
10 
11 // Call the ShowResults callback function for each
12 // array element.
13 letters.forEach(ShowResults);
14 
15 // Output:
16 //  value: ab index: 0 
17 //  value: cd index: 1 
18 //  value: ef index: 2 

在下面的示例中,callbackfn 参数包含回调函数的代码。

 1 // Create an array.
 2 var numbers = [10, 11, 12];
 3 
 4 // Call the addNumber callback function for each array element.
 5 var sum = 0;
 6 numbers.forEach(
 7     function addNumber(value) { sum += value; }
 8 );
 9 
10 document.write(sum);
11 // Output: 33

下面的示例阐释了 thisArg 参数的用法,该参数指定可对其引用 this 关键字的对象。

 1 // Define the object that contains the callback function.
 2 var obj = {
 3     showResults: function(value, index) {
 4         // Call calcSquare by using the this value.
 5         var squared = this.calcSquare(value);
 6 
 7         document.write("value: " + value);
 8         document.write(" index: " + index);
 9         document.write(" squared: " + squared);
10         document.write("<br />");
11     },
12     calcSquare: function(x) { return x * x }
13 };
14 
15 // Define an array.
16 var numbers = [5, 6];
17 
18 // Call the showResults callback function for each array element.
19 // The obj is the this value within the 
20 // callback function.
21 numbers.forEach(obj.showResults, obj);
22 
23 // Embed the callback function in the forEach statement.
24 // The obj argument is the this value within the obj object.
25 // The output is the same as for the previous statement.
26 numbers.forEach(function(value, index) { this.showResults(value, index) }, obj);
27 
28 // Output:
29 //  value: 5 index: 0 squared: 25
30 //  value: 6 index: 1 squared: 36
31 //  value: 5 index: 0 squared: 25
32 //  value: 6 index: 1 squared: 36

要求

在以下文档模式中受支持:Internet Explorer 9 标准模式、Internet Explorer 10 标准模式和 Internet Explorer 11 标准模式。此外,也在应用商店应用(Windows 8 和 Windows Phone 8.1)中受支持。请参阅版本信息

在以下文档模式中不受支持:Quirks、Internet Explorer 6 标准模式、Internet Explorer 7 标准模式、Internet Explorer 8 标准模式。

实例:

1 var data=[1,2,3,4,5,6];
2 var sum=0;
3 data.forEach(function(v){//其中的v就是数组的值 123456
4 sum+=v;})
5 document.write(sum+"<br>");//打印出来是21
6 data.forEach(function(o,p,q){//分别对应:数组元素,元素的索引,数组本身
7  q[p]=o+1;
8 })
9 document.write(data);

注意:forEach无法在所有元素都传递给调用的函数之前终止(而for循环却有break方法),如果要提前终止,必须把forEach放在try块中,并能抛出一个异常。如果forEach()调用的函数抛出foreach.break异常,循环会提前终止:

 1 function foreach(a,b,c){
 2  try{
 3   a.forEach(b,c);
 4 }catch(e){
 5   if(e===foreach.break)return;
 6  else throw e;
 7 }
 8 }
 9 foreach.break=new Error("StopIteration");
10  
11 }

 

转载于:https://www.cnblogs.com/joyco773/p/6113748.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值