Performs the specified action for each element in an array.
SyntaxforEach( callbackfn [, thisArg ])
The following example illustrates use of the forEach method.
function ShowResults(value, index, ar) {
document.write("value: " + value);
document.write(" index: " + index);
document.write("<br />");
}
var letters = ['ab', 'cd', 'ef'];
letters.forEach(ShowResults);
In the following example, the callbackfn argument includes the code of the callback function.
var numbers = [10, 11, 12];
var sum = 0;
numbers.forEach(
function addNumber(value) { sum += value; }
);
document.write(sum);
The following example illustrates the use of the thisArg argument, which specifies an object that can be referred to with the this keyword.
var obj = {
showResults: function(value, index) {
var squared = this.calcSquare(value);
document.write("value: " + value);
document.write(" index: " + index);
document.write(" squared: " + squared);
document.write("<br />");
},
calcSquare: function(x) { return x * x }
};
var numbers = [5, 6];
numbers.forEach(obj.showResults, obj);
numbers.forEach(function(value, index) { this.showResults(value, index) }, obj);
Remarks
The forEach method calls the callbackfn function one time for each element present in the array, in ascending index order. The callback function is not called for missing elements of the array.
In addition to array objects, the forEach method can be used by any object that has a length property and that has numerically indexed property names.
Callback Function Syntax
The syntax of the callback function is as follows:
function callbackfn(value, index, array1)
You can declare the callback function by using up to three parameters.
The callback function parameters are as follows.
Callback argument Definition value The value of the array element. index The numeric index of the array element. array1 The array object that contains the element.The forEach method does not directly modify the original array, but the callback function might modify it. The following table describes the results of modifying the array object after the forEach method starts.
Condition after forEach method starts Element passed to callback function? Element is added beyond the original length of the array. No. Element is added to fill in a missing element of the array. Yes, if that index has not yet been passed to the callback function. Element is changed. Yes, if that element has not yet been passed to the callback function. Element is deleted from the array. No, unless that element has already been passed to the callback function. ExceptionsIf the callbackfn argument is not a function object, a TypeError exception is thrown.
See also Specification[15.4.4.18 Array.prototype.forEach ( callbackfn , thisArg )] ECMAScript® Language Specification Standard ECMA-262 5.1 Edition / June 2011
AttributionsMicrosoft Developer Network: Article
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4