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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM