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

How to use forEach in js and what is the difference between forEach and for

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

Share

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

This article introduces the relevant knowledge of "how to use forEach in js and what is the difference between forEach and for". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Definition and usage

ForEach () calls each element of the array and passes the element to the callback function.

Note: forEach () does not execute callback functions for empty arrays.

Usage:

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

1 currentValue = > required. Current element

2 optional = > index. The index value of the current element, which is of numeric type

3optional = > arr. The array object to which the current element belongs

4optional. The value passed to the function usually uses the "this" value.

If this parameter is empty, "undefined" is passed to the "this" value.

ForEach's attention points:

ForEach () itself does not support continue and break statements.

We can use return statements to achieve the effect of the continue keyword

Second, use scenario 1. Scenarios used (calculating the sum of numbers)

Calculate the sum of all the elements of the array:

Let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9]; let sum= 0 position arr.forEach ((currentIndex, index, curArr) = > {sum+ = currentIndex / / sum=sum+currentIndex}) console.log ('sum is', sum); 2. The scenario used (add a key value to the original array) / / add an attribute value let arr = [{id: '001values, name:' Zhang San 1'}, {id: '002values, name:' Zhang San 2'}, {id: '003numbers, name:' Zhang San 2'}] / / add an attribute value arr.forEach ((item, index) = > {item ['addAttrs'] ='}) console.log ('arr', arr) to each entry of the original array;-- use for of for processing-- for (let item of arr) {item [' index'] =''} console.log ('arr', arr) 3. ForEach jumps out of the loop 1.forEach jumps out of the current loop return// content is 3, does not traverse the item var arr = [1,2,3]; arr.forEach (function (item) {if (item = 3) {return;} console.log (item);}); 2.forEach combined with try jumps out of the whole loop

Find that id is 002, then terminate the entire loop and return the value of the current item.

Done using try-catch

The code is as follows:

Let arr = [{id: '001percent, name:' Zhang San 1'}, {id: '002percent, name:' Zhang San 2'}, {id: '003percent, name:' Zhang San 2'}] / / use forEach to jump out of the loop and use rty-catchfunction useForeach (Arr) {let obj = {} try {Arr.forEach (function (item) {if (item.id = = '002') {/ / to find the target item and assign it. Then throw the exception obj = item throw new Error ('return false')}};} catch (e) {/ / return the data of this term of id===002 return obj}} console.log (useForeach (arr)) 3.forEach and for loop difference [interview questions]

1 iteration = > for can skip an iteration in the current loop with continue, and forEach will report an error with continue. But you can use return to jump out of the current loop

2 jump = > for can use break to jump out of the whole loop, and forEach normally cannot jump out of the whole loop.

If the interviewer asks: if you have to jump out of the loop in forEach, you can throw an exception to handle it.

This is the end of the content of "how to use forEach in js and what is the difference between forEach and for". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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