In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "how to use forEach () in JavaScript". The content is simple and clear. I hope it can help you solve your doubts. Let the editor lead you to study and learn this article "how to use forEach () in JavaScript".
ForEach ()
Foreach statement is one of the new features of java5. Foreach provides great convenience for developers in traversing arrays and collections.
The foreach statement is a special simplified version of the for statement, but the foreach statement cannot completely replace the for statement. However, any foreach statement can be rewritten to the for statement version.
Foreach is not a keyword, and this special for statement format is conventionally called a "foreach" statement. Understand the literal meaning of foreach, that is, "for everyone" in English.
Await cannot be used in forEach: the reason: that is, forEach only supports synchronized code.
The forEach loop does not jump out, and neither the break command nor the return command works
The forEach () method executes the provided function once for each element of the array.
Var arr = ['await,' baked,'c']
Arr.forEach (function (e) {
Console.log (e)
});
/ / output the result
A
B
C
The function callback in the forEach method takes three parameters:
The first parameter is the current element of the traversal (required)
The second parameter is the index value of the current element (optional)
The third parameter is the array object to which the current element belongs (optional)
ForEach (function (value,index,array) {
/ / something
})
Var arr = [1, 2, 3, 4]
Arr.forEach (console.log)
/ / equivalent to:
Var arr = [1, 2, 3, 4]
For (var k = 0; k
< arr.length; k++) { console.log(array[k]); } var arr = [1,2,3,4]; var sum =0; arr.forEach(function(value,index,array){ array[index] == value; //输出结果为 true sum+=value; }); console.log(sum); //输出结果 10 forEach 循环无法中途跳出,break 命令或 return 命令都不能奏效 1、forEach不支持break 大家都知道,在使用for循环时可以使用break跳出循环,比如我希望找到数组中符合条件的第一个元素就跳出循环,这对于优化数组遍历是非常棒的。很遗憾,forEach并不支持break操作,使用break会导致报错。 let arr = [1, 2, 3, 4]; for (let i = 0; i < arr.length; i++) { console.log(arr[i]); if (arr[i] === 2) { break; }; }; //输出结果 1,2 arr.forEach((self, index) =>{
Console.log (self)
If (self = 2) {
Break
}
});
/ / report an error
2. It is invalid to use return in forEach
First of all, you need to make sure that using return directly in the for loop will report an error (using for in the function can return). Using return in forEach will not report an error, but rerutn will not take effect. Let's take a look at an example:
Let arr = [1, 2, 3, 4]
Function find (array, num) {
Array.forEach ((value, index) = > {
If (value = num) {
Return index
}
});
}
Let index = find (arr, 2)
Console.log (index)
/ / output result undefined
The above code wants to find the index of the number 2 in the array, but return does not stop the code from running and return a value.
Of course, if we really want to return a value with return, we can only put the return operation in a function, not in a forEach loop, like this:
Let arr = [1, 2, 3, 4]
Function find (array, num) {
Let index_
Array.forEach ((value, index) = > {
If (value = num) {
Index_ = index
}
});
Return index_
}
Console.log (find (arr, 2))
/ / output result 1
3. ForEach deletes its own element index will not be reset
Remember the question at the beginning of the article, that code will only be executed once, and the array will not be deleted, because forEach implicitly causes index to augment itself after traversing the callback function, like this:
Let arr = [1, 2, 3, 4]
Arr.forEach ((value, index) = > {
Arr.splice (index, 1)
/ / implicitly let index increase itself by 1.
Index++
});
Console.log (arr)
/ / output result [2pc4]
4. ForEach does not change the original array:
Let arr = [1,2,3,4,5]
Let a = arr.forEach (function (value, index, arr) {
Console.log (value)
/ / output
})
Console.log (a) / / output result undefined
Console.log (arr.toString ()) / / output 1, 2, 3, 4, 5
The difference between for and forEach
A for loop can jump out of a loop using break, but forEach cannot.
The for loop can control the start of the loop (the number initialized by I determines the start of the loop), and forEach can only start at index 0 by default.
Modifying the index (modify I) is supported during the for loop, but forEach cannot (the underlying control index increments itself, and we can't control it).
The above is all the contents of the article "how to use forEach () in JavaScript". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more 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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.