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's Array.forEach () for?

2025-04-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces what is the use of Array.forEach (). It is very detailed and has a certain reference value. Interested friends must finish reading it!

Arr .forEach (callback (currentValue [, index [, array]]) [, thisArg])

The functions that callback performs on each element. It accepts one to three parameters:

The current element being processed in the currentValue array.

The index in the index optional currentValue array.

The array optional array forEach () is called.

The value callback that the thisArg optional this is used as when executed.

Flattened array

Function flatten (arr) {

Const result = []

Arr.forEach ((I) = > {

If (Array.isArray (I)) {

Result.push (... flatten (I))

} else {

Result.push (I)

}

})

Return result

}

/ / Usage

Const nested = [1,2,3, [4,5, [6,7], 8,9]]

Flatten (nested) / / [1,2,3,4,5,6,7,8,9]

The above is all the content of the article "what is the use of Array.forEach ()". Thank you for reading! Hope to share the content to help you, more related knowledge, 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