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/03 Report--
This article mainly explains "how to traverse arrays in JavaScript". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn how to traverse arrays in JavaScript.
The method of traversing the array in JavaScript: 1, use the for loop statement; 2, use the forEach () method to call the callback function; 3, use the map () method to call the callback function; 4, use the "for..in" loop statement; 5, use the "for … of" loop statement.
The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.
When writing code in JavaScript, you can use several methods to traverse the array, including for loop, forEach loop, map loop, forIn loop, forOf loop, and so on.
1. For cycle: basic and simple
This is the most basic and commonly used method of traversing arrays; it is generally supported by various development languages.
Let arr = ['a let I = 0, len = arr.length; I];
< len; i++) { console.log(i); // 0 1 2 3 4 console.log(arr[i]); //a b c d e} 二、forEach() 方法:使用回调函数 forEach() 这是数组对象的一个方法;其接受一个回调函数为参数。 回调函数中有三个参数: 1st:数组元素(必选) 2nd:数组元素索引值(可选) 3rd:数组本身(可选) let arr = ['a','b','c','d','e'];arr.forEach((item,index,arr)=>{console.log (item); / / a b c de console.log (index); / / 0 1 2 3 4 console.log (arr); / / ['axiaqingmianzhongyuanzhongyuanzhongyuanzhongyuanzhongyuanzhongyuanzhong "]})
Third, map () method: use callback function
It is used in the same way as the forEach () method.
Var arr = [{name:'a',age:'18'}, {name:'b',age:'19'}, {name:'c',age:'20'}]; arr.map (function (item,index) {if (item.name = ='b') {console.log (index) / / 1}})
4. For..in loop: traversing objects and arrays
For... In loops can be used to loop objects and arrays.
It is recommended for circular objects and can also be used to traverse json.
Let obj = {name: 'king sledgehammer', age: '18kg, weight:' 70kg'} for (var key in obj) {console.log (key); / / name age weight console.log (objkey); / / Wang sledgehammer 18 70kg}-- let arr = ['axiaqingzhongzhong'] For (var key in arr) {console.log (key); / 0 1 2 3 4 returns the array index console.log (ARR [key]) / / a b c d e}
5. For... Of loop: traversing objects and arrays
Loopable arrays and objects, recommended for traversing arrays.
For... Of provides three new methods:
Key () is a traversal of key names
Value () is a traversal of key values
Entries () is a traversal of key-value pairs
Let arr = ['iFLYTEK', 'political and legal BG',' front-end development']; for (let item of arr) {console.log (item); / / iFLYTEK political and legal BG front-end development} / / output array index for (let item of arr.keys ()) {console.log (item) / / 0 12} / / output content and index for (let [item, val] of arr.entries ()) {console.log (item +':'+ val); / / 0: iFLYTEK 1: political and legal BG 2: front-end development}
VI. Supplement
6.1, break and Continue issu
The break and continue keywords are not valid in the forEach, map, filter, reduce, every, and some functions because they are in function, but function solves the problem of closure traps.
To use break and continue, you can use for, for...in, for...of, while.
6.2, arrays and objects
Used to traverse array elements using: for (), forEach (), map (), for...of.
Used for looping object properties using: for...in.
Thank you for your reading, the above is the content of "how to traverse the array in JavaScript". After the study of this article, I believe you have a deeper understanding of how to traverse the array in JavaScript, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.