In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-22 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
The content of this article mainly focuses on what is the method used to achieve the cycle in javascript. The content of the article is clear and clear. It is very suitable for beginners to learn and is worth reading. Interested friends can follow the editor to read together. I hope you can get something through this article!
The method to realize the loop: 1, for loop statement; 2, "for in" loop statement; 3, while loop statement; 4, "do while" loop statement; 5, forEach () method; 6, map () method; 7, filter () method; 8, some (); 9, every (), and so on.
The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.
12 Loop traversal methods in JavaScript
1. For cycle
Let arr = [1mem2jue 3]; for (let ionomy 0; i0) / / 3max / 2pm / 1
The do while loop is a variant of the while loop, which first performs an operation before making a condition judgment, and then continues with the operation if it is true, and the loop ends if it is false.
5. Array forEach cycle
Let arr = [1Jing 2pm 3]; arr.forEach (function (iMagin index) {console.log (iMagin index)}) / / 10pm / 21max / 3 2
ForEach loop, loops through each element of the array and takes actions. There is no return value, so you don't need to know the length of the array. It has three parameters, only the first one is required, representing the value under the current subscript.
In addition, please note that the forEach loop can not be stopped until all the elements are called, it does not have a break statement, if you have to stop, you can try the try catch statement, that is, to force the exit, throw an error to catch to capture, and then return in catch, so that you can stop the loop, if you often use this method, it is best to customize such a forEach function in your library.
6. Array map () method
Let arr = [1meme 2jue 3]; let tt = arr.map (function (I) {console.log (I) return iTunes 2;}) / / [2meme 4je 6]
The map () method returns a new array whose elements are the values of the original array element after calling the function.
Note: both the map and forEach methods can only be used to traverse arrays, not ordinary objects.
7. Array filter () method
Let arr = [1Jing 2jue 3]; let tt = arr.filter (function (I) {return I > 1;}) / / [2Jue 3]
The filter method is an Array object built-in method that returns the filtered elements without changing the original array.
8. Array some () method
Let arr = [1 return 2 and 3]; let tt = arr.some (function (I) {return I > 1;}) / / true
The some () method is used to detect whether the elements in the array meet the specified condition (provided by the function) and returns the boolean value without changing the original array.
9. Array every () method
Let arr = [1Jing 2Jue 3]; let tt = arr.some (function (I) {return I > 1;}) / / check whether the elements in the array are all greater than 1max / false
The every () method is used to detect whether all elements of the array meet the specified condition (provided by the function) and returns the boolean value without changing the original array.
10. Array reduce () method
Let arr = [1meme 2jue 3]; let ad = arr.reduce (function (iMagazine j) {return itemj;}) / / 6
The reduce () method takes a function as an accumulator, and each value in the array (from left to right) starts to shrink and eventually evaluates to a value.
11. Array reduceRight () method
Let arr = [1meme 2jue 3]; let ad = arr.reduceRight (function (iMagazine j) {return itemj;}) / / 6
The reduceRight () method, which is the same as reduce (), is calculated forward from the end of the array.
12. For of cycle
Let arr = ['name','age']; for (let i of arr) {console.log (I)} / / name// age
For of Loop is a new statement in Es6 to replace for in and forEach. It allows you to traverse iterated (Iterable data) data structures such as Arrays (array), Strings (string), Maps (mapping), Sets (collection), and pay attention to its compatibility.
Thank you for your reading. I believe you have some understanding of "what are the methods used to achieve loops in javascript?" go ahead and practice it. If you want to know more about it, you can follow the website! The editor will continue to bring you better articles!
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.