Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What does the forEach () method do in javascript?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article shows you what the forEach () method in javascript does, which is concise and easy to understand. It will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

In javascript, the forEach () method is used to iterate over an array, calling each element of the array and passing it to the callback function for processing; the syntax "array.forEach (function (Value,index,arr), thisValue)".

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

The Array type defines the forEach () prototype method for each array, which allows you to perform iterations for the array.

The forEach () method invokes each element of the array and passes the element to the callback function.

The specific syntax is as follows:

Array.forEach (function (currentValue, index, arr), thisValue)

The parameters are described as follows:

Array: an array object.

Function (currentValue, index, arr): a function that is required to accept up to three arguments. 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, an 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 calls the callbackfn function once, in ascending indexing order, but does not call the callback function for the array's empty elements.

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

The forEach method does not directly modify the original array, but the callback function may modify it. The result of modifying the array object after the forEach method starts is shown in the table.

The callback function modifies the influence of the array whether the conditional element after the forEach method starts is passed to the callback function to add an element beyond the original length of the array No add an element to fill the missing element in the array is, if the index has not been passed to the callback function element has been changed, if the element has not been passed to the callback function to remove the element from the array, unless the element has been passed to the callback function

Example 1: iterate over the array a using forEach, and then display the value and subscript index output of each element, as follows:

Function f (value,index,array) {console.log ("a [" + index + "] =" + value);} var a = ['a', 'baked,' c']; a.forEach (f)

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

Var a = [10,11,12], sum = 0boma.forEach (function (value) {sum + = value;}); console.log (sum); / / return 33

Output:

The above content is what the forEach () method does in javascript. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report