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

Summary of methods for traversing arrays in JavaScript

黄舟
Release: 2017-11-09 14:59:15
Original
1657 people have browsed it

I believe everyone knows the role of arrays. It manages the storage space of multiple data in batches. Arrays have a long data structure, which can greatly improve the execution efficiency of programs, whether in PHP or ## In #JavaScript or in jQuery, the form of arrays is divided into index arrays and associative arrays, so how to traverse the array! Below we will give you a detailed introduction toJavaScript arrayTraversal~

1.for(){}Traversing the array

Copy after login

2.for in loop traverses the array





Copy after login

3.forEach methodNote: the forEach() method is introduced by the ES5.1 standard.

var arr=[1,2,3,4,5,6];
arr.forEach(function(v,i){//v==value 为arr项,i==index 为arr索引
    console.log(i+'  'v );
})//输出0  11  22  33  44  55  6
Copy after login

4. A new method has been added in the latest ES6 standard==for of==

for of method  You can use subscript loops when traversing Array, but you cannot use subscripts when traversing Map and Set. Map, set – no subscript, cannot use index to traverse
In order to unify collection types, the ES6 standard introduces a new iterable type,
Array, Map and Set all belong to iterable types.
==Collections with iterable types can be traversed through the new for ... of loop. ==

var arr=[1,2,3,4,5,6];for(var value of Arr){
    console.log(value);
}  //输出123456
Copy after login

Summary:

The JavaScript method of traversing arrays has been summarized. Do you believe that you are familiar with arrays? We have a certain understanding of traversal , but in our actual development, we still need to choose according to our own needs, because the execution efficiency of some methods is not the best! So use with caution!

Related recommendations:

1.Detailed explanation of the difference between javascript array traversal for and for in

2.php array and js array traversal

The above is the detailed content of Summary of methods for traversing arrays in JavaScript. 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!