In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to use the iterative method in JavaScript". In the daily operation, I believe that many people have doubts about how to use the iterative method in JavaScript. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use the iterative method in JavaScript". Next, please follow the editor to study!
Iterative methods in JavaScript: 1, every (), to query whether each element in the array satisfies a certain condition; 2, some (), to query whether each element in the array satisfies certain conditions; 3, filter (); 4, map (); 5, forEach (); 6, reduce ().
The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.
Definition:
1. Iterate, which refers to accessing each item in the list one by one in a certain order. For example, the for statement.
2. Loop, which refers to the repeated execution of the same piece of code if the conditions are met. For example, the while statement.
3. Traversal means that each node in the tree structure is accessed according to certain rules, and each node is visited only once.
4. Recursion refers to the behavior of a function calling itself constantly. For example, output the famous Fibonacci sequence programmatically.
Iterative methods in JavaScript:
1. Every ()
Used to query whether each of the arrays satisfies a certain condition
Var num = [1, 2, 3, 4, 5, 6, 7, 8, 9]; var number = num.every (function (item,index) {return (item > 5); / / determine whether all incoming values are greater than 5}) console.log (number); / / false is false as long as one is not satisfied
2. Some ()
Each item in the query array satisfies certain conditions
Var num = [1, 2, 3, 4, 5, 6, 7, 8, 9]; var number = num.every (function (item,index) {return (item > 5); / / determine which of the incoming values is greater than 5}) console.log (number); / / false is true as long as one is satisfied
3. Filter ()
Filter items that meet the criteria to form a new array
Var num = [1, 2, 3, 4, 5, 6, 7, 8, 9]; var number = num.filter (function (item,index) {return (item > 5); / / determine which incoming values are greater than 5}) console.log (number); / / [6, 7, 8, 9]
4. Map ()
Form a new array by calculating the items in the original array
Var num = [1, 2, 3, 4, 5, 6, 7, 8, 9]; var number = num.map (function (item,index) {return item-1; / / determine which of the incoming values is greater than 5}) console.log (number); / / [0,1,2,3,4,5,6,7,8]
5. ForEach ()
Pass in each item of the array
Var num = [1,2,3,4,5,6,7,8,9]; var number = num.forEach (function (item,index) {console.log (item); / / [1,2,3,4,5,6,7,8,9]})
6. Reduce ()
Calculate the cumulative values of the front and back items of the array
Var num = [1, 2, 3, 4, 5, 6, 7, 8, 9]; var number = num.reduce (function (prev,cur,index) {return prev + cur;}) console.log (number); / / 1 is essentially the process of accumulating items in an array.
The iterative method of forEach is essentially no different from the for loop, but three parameters are passed in when using this method, and the first item, that is, each item in the output array, is passed in the output. If the second item is passed in, then the subscript value of the index of the array is iterated, and if the third item is passed, the entire num array is iterated.
At this point, the study on "how to use the iterative method in JavaScript" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical 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.