Home > Web Front-end > Front-end Q&A > What does the forEach() method in javascript do?

What does the forEach() method in javascript do?

青灯夜游
Release: 2021-10-13 17:08:38
Original
2279 people have browsed it

In JavaScript, the forEach() method is used to perform iterative operations for arrays. Each element of the array can be called and the elements are passed to the callback function for processing; the syntax "array.forEach(function(Value, index,arr),thisValue)".

What does the forEach() method in javascript do?

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.

The Array type defines the forEach() prototype method for each array, which can be used to perform iteration operations on the array.

The forEach() method is used to call each element of the array and pass the element to the callback function.

The specific syntax is as follows:

array.forEach(function(currentValue, index, arr), thisValue)
Copy after login

The parameter description is as follows:

  • array: an array object.

  • function(currentValue, index, arr): Required parameters, a function that can receive up to three parameters. forEach will call the callbackfn function once for each element in the array.

    • currentValue: required. Current element

    • #index: optional. The index value of the current element.

    • #arr: Optional. The array object to which the current element belongs.

  • thisArg: Optional parameter, the object that can be referenced by this in the callbackfn function. If thisArg is omitted, the value of this is undefined.

For each element that appears in the array, the forEach method will call the callbackfn function once, in ascending index order, but will not call the callback function for empty elements in the array.

In addition to array objects, the forEach method can also be used on any object that has a length property and has a numerically indexed property name, such as associative array objects, Arguments, etc.

The forEach method does not directly modify the original array, but the callback function may modify it. The results obtained by modifying the array object after the forEach method is started are as shown in the table.

The impact of the callback function modifying the array
Conditions after the forEach method is startedWhether the element is passed to the callback function
Add elements beyond the original length of the arrayNo
Add elements to fill missing elements in the array Yes, if the index has not been passed to the callback function
The element has changedYes, if the element has not been passed to the callback function
Remove element from arrayNo, unless the element has been passed to the callback function

Example 1: Use forEach Iterate over the array a, and then output and display the value and subscript index of each element. The code is as follows:

function f(value,index,array) {
    console.log("a[" + index + "] = " + value);
}
var a = ['a', 'b', 'c'];
a.forEach(f);
Copy after login

Output:

What does the forEach() method in javascript do?

Example 2: Use forEach to iterate the array a, then calculate the sum of the array elements and output

var a = [10, 11, 12], sum = 0;
a.forEach (function (value) {
    sum += value;
});
console.log(sum);  //返回33
Copy after login

Output:

What does the forEach() method in javascript do?

[Recommended learning:javascript advanced tutorial

The above is the detailed content of What does the forEach() method in javascript do?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template