Home > Web Front-end > JS Tutorial > body text

Summary of JS array traversal methods

零到壹度
Release: 2018-04-21 15:24:17
Original
1613 people have browsed it

JS array traversal must be used in the project. In order to write elegant code, use the method in the right place. Here we will compare several methods and display them in es6. , you need to build a conversion es5 environment. This is not the focus of this article. You can write about this in the next article.

1. The for loop

is the most frequently used and is used by the front-end, indicating that I will only use the for loop at the beginning. . .

let arr = [&#39;123&#39;, &#39;456&#39;, &#39;789&#39;];for (let i = 0; i < arr.length; i ++) {
    console.log(arr[i]);
}
Copy after login

The most commonly used, but there is room for optimization:

for (let i = 0, len = arr.length; i < len; i ++) {
    console.log(arr[i]);
}
Copy after login

Use temporary variables to store the length value to avoid repeatedly obtaining the array length.

2. for...of loop

The new loop method in es6 is simpler and more efficient than the for loop in es5. It also provides three new methods:

  1. key() is a traversal of key names;

  2. value() is a traversal of key values;

  3. ##entries () is a traversal of key-value pairs;

  4. let arr = [&#39;科大讯飞&#39;, &#39;政法BG&#39;, &#39;前端开发&#39;];
    for (let item of arr) {  
      console.log(item);
    }
    // 输出数组索引
    for (let item of arr.keys()) {  
      console.log(item);
    }
    // 输出内容和索引
    for (let [item, val] of arr.entries()) {  
      console.log(item + &#39;:&#39; + val);
    }
    Copy after login
3. foreach loop

The foreach method of the array is used more frequently, but its performance is weaker than the for loop. It has the advantage that empty array elements can be automatically omitted, which is equivalent to automatically emptying.

let arr = [&#39;科大讯飞&#39;, ,  &#39;政法BG&#39;, , &#39;前端开发&#39;];
arr.forEach((val,index)=>console.log(index,val));
Copy after login

4. Filter loop

has a loop function, which is mainly used to filter arrays. It receives a method and it will return a collection of elements that conform to the function.

let arr = [{
    label: &#39;科大讯飞&#39;,    
    value: 1
}, {
    label: &#39;政法BG&#39;,    value: 2
}, {
    label: &#39;前端开发&#39;,    value: 3
}];

const arr1 = arr.filter(list => list.value === 1);
console.log(arr1);
Copy after login

5. Some loop

has the same function as filter. The difference is that it returns a boolean value, which is used to check whether an object exists in the array.

if (arr.some(list => list.value === 1)) {
    console.log(&#39;执行了!&#39;)
}
Copy after login

So it is often used in if.

6. Map loop

The role of substitution, it will return a set of values ​​returned after calling the callback function for each element in the original array.

let arr = [1, 2, 3, 4];
onst arr1 = arr.map(list => list * 2);
console.log(arr1);
Copy after login
The above are the commonly used array traversal methods. In the future, you can use one method according to the actual situation, instead of using for loops everywhere.


Related recommendations:

Common methods for traversing arrays

3 ways to traverse list collections

Detailed explanation of HashMap in Java8 (storage structure, function implementation, expansion optimization, thread safety , traversal method)

Revisit the data structure: common methods of binary trees and three traversal methods Java implementation


The above is the detailed content of Summary of JS array traversal methods. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!