Five ways to traverse JavaScript arrays

Five ways to traverse JavaScript arrays

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 simple

This 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.
There are three parameters in the callback function:

  • 1st: Array element (required)
  • 2nd: array element index value (optional)
  • 3rd: the array itself (optional)
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 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

The for…in loop can be used to loop over objects and arrays.
Recommended for looping objects, can also be used to traverse json.

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 arrays

It is recommended for looping over arrays.

for...of provides three new methods:

  • key() is a traversal of the key name;
  • value() is a traversal of key values;
  • entries() is a traversal of key-value pairs;
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 forEach、map、filter、reduce、every、some functions, break and continue keywords will not take effect because they are in function, but function solves the problem of closure trap.
To use break or continue, you can use for、for...in、for...of、while .

6.2 Arrays and Objects

To iterate over array elements, use: for(),forEach(),map(),for...of .
To loop over object properties use: for...in .

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:
  • Several commonly used array traversal methods in JS and detailed examples of performance analysis and comparison
  • Example analysis of javascript array traversal method
  • In-depth analysis of JS array traversal method (recommended)
  • Introduction to array traversal forEach() and map() methods in JavaScript and their compatible writing
  • Detailed explanation of the difference between javascript array traversal for and for in
  • Discussion on the differences between IE8's JS parsing through attributes and array traversal
  • javascript browser determines binding event arguments conversion array array traversal
  • The usage and advantages and disadvantages of for, for in, for of, map, forEach in JS array traversal

<<:  MySQL green version setting code and 1067 error details

>>:  How to use Dockerfile to create a mirror of the Java runtime environment

Recommend

Docker data volume container creation and usage analysis

A data volume container is a container specifical...

Defining the minimum height of the inline element span

The span tag is often used when making HTML web p...

Key knowledge summary of Vue development guide

Table of contents Overview 0. JavaScript and Web ...

Linux system MySQL8.0.19 quick installation and configuration tutorial diagram

Table of contents 1. Environment Introduction 2. ...

Share JS four fun hacker background effect codes

Table of contents Example 1 Example 2 Example 3 E...

Data URI and MHTML complete solution for all browsers

Data URI Data URI is a scheme defined by RFC 2397...

Vue+el-table realizes merging cells

This article example shares the specific code of ...

How to design the homepage of Tudou.com

<br />I have been working in front-end for s...

Detailed explanation of the role of static variables in MySQL

Detailed explanation of the role of static variab...

About MYSQL, you need to know the data types and operation tables

Data Types and Operations Data Table 1.1 MySQL ty...

Detailed explanation of CocosCreator Huarongdao digital puzzle

Table of contents Preface text 1. Panel 2. Huaron...

Solutions for high traffic websites

First: First, confirm whether the server hardware ...

The most detailed method to install docker on CentOS 8

Install Docker on CentOS 8 Official documentation...

Example of using CSS3 to achieve shiny font effect when unlocking an Apple phone

0. Introduction August 18, 2016 Today, I noticed ...

Detailed explanation of MySQL backup and recovery practice of mysqlbackup

1. Introduction to mysqlbackup mysqlbackup is the...