When writing code in JavaScript, you can use multiple methods to traverse an array, including for loops, forEach loops, map loops, forIn loops, and forOf loops. 1. for loop: basic and simpleThis is the most basic and commonly used method of traversing an array; various development languages generally support this method. let arr = ['a','b','c','d','e']; for (let i = 0, len = arr.length; i < len; i++) { console.log(i); // 0 1 2 3 4 console.log(arr[i]); //abcde } 2. forEach() method: using callback function forEach() This is a method of the array object; it accepts a callback function as a parameter.
let arr = ['a','b','c','d','e']; arr.forEach((item,index,arr)=> { console.log(item); // abcde console.log(index); // 0 1 2 3 4 console.log(arr); // ['a','b','c','d','e'] }) 3. map() method: using callback functionIt 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 The for…in loop can be used to loop over objects and arrays. let obj = { name: 'Wang Dachui', age: '18', weight: '70kg' } for(var key in obj) { console.log(key); // name age weight console.log(obj[key]); // Wang Dachui 18 70kg } ---------------------------- let arr = ['a','b','c','d','e']; for(var key in arr) { console.log(key); // 0 1 2 3 4 returns the array index console.log(arr[key]) // abcde } 5. for…of loop: traversing objects and arraysIt is recommended for looping over arrays. for...of provides three new methods:
let arr = ['iFLYTEK', 'Politics and Law BG', 'Front-end Development']; for (let item of arr) { console.log(item); // iFLYTEK Politics and Law BG front-end development} // Output array index for (let item of arr.keys()) { console.log(item); // 0 1 2 } // Output content and index for (let [item, val] of arr.entries()) { console.log(item + ':' + val); // 0: iFLYTEK 1: Politics and Law BG 2: Front-end Development} 6. Supplement 6.1. Break and Continue Issues In 6.2 Arrays and Objects To iterate over array elements, use: The above are the details of the five methods of JavaScript array traversal. For more information about JavaScript array traversal, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: MySQL green version setting code and 1067 error details
>>: How to use Dockerfile to create a mirror of the Java runtime environment
Table of contents Application Scenario Simply put...
1. The mysqldump backup method uses logical backu...
The cause is that the process opens a number of f...
Networks usage tutorial Official website docker-c...
Click here to return to the 123WORDPRESS.COM HTML ...
1. Embed is illegal The <embed> tag is a pri...
Preface We know that index selection is the work ...
Table of contents Class component event binding F...
It is essentially a common js object used to desc...
1.1 Introduction to iptables firewall Netfilter/I...
Implementation of time comparison in MySql unix_t...
I have a product parts table like this: part part...
Someone asked me before whether it is possible to...
public function json_product_list($where, $order)...
Docker daemon socket The Docker daemon can listen...