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

How to perform array traversal in js? 3 methods of one-dimensional array traversal in js (detailed explanation with pictures and texts)

青灯夜游
Release: 2018-10-16 14:00:28
Original
4958 people have browsed it

How to traverse js array? This article will introduce to you how js traverses one-dimensional arrays, and let you know the three methods of one-dimensional array traversal in js. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

One-dimensional array traversal in js can basically be implemented using some methods such as for, forin, foreach, forof, map, etc. Below we will focus on the three types of array traversal: for, forin, and foreach. How the method is implemented is explained through a simple code example.

Using for loop in jsTraverse the array
##



	
		
	

Copy after login

Rendering (traversal result):


How to perform array traversal in js? 3 methods of one-dimensional array traversal in js (detailed explanation with pictures and texts)

Brief description:

The for loop is the simplest traversal method and the most frequently used traversal method.

Using for in loop in jsTraverse the array



	
		
	

Copy after login

Rendering (traversal result):

How to perform array traversal in js? 3 methods of one-dimensional array traversal in js (detailed explanation with pictures and texts)

Brief description:

for...in loop needs two formal parameters to traverse the array, index represents the subscript of the array (can be customized), and arr represents the traversal array.

The for...in loop is a method that many people like to use, but in fact, analysis and testing have found that among the many loop traversal methods, its efficiency is the lowest.

Using foreach loop in jsTraverse the array



	
		
	

Copy after login

Rendering (traversal result):

How to perform array traversal in js? 3 methods of one-dimensional array traversal in js (detailed explanation with pictures and texts)

Brief description:

The foreach loop that comes with the array is used more frequently, but its performance is actually weaker than the ordinary for loop.

The foreach loop has two parameters. The first parameter (ele, which can be customized) represents the element in the array, and the second parameter (index, which can be customized) represents the subscript of the array.

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study. For more related tutorials, please visit

JavaScript Video Tutorial, jQuery Video Tutorial, bootstrap Tutorial!

The above is the detailed content of How to perform array traversal in js? 3 methods of one-dimensional array traversal in js (detailed explanation with pictures and texts). For more information, please follow other related articles on the PHP Chinese website!

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!