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

JS array traversal methods for loop and for...in

巴扎黑
Release: 2016-11-25 13:39:06
Original
1465 people have browsed it

There are two ways to traverse JS arrays:

The first one: a general for loop, for example:

?

var a = new Array("first", "second", "third")

for( var i = 0;i < a.length; i++) {

document.write(a[i]+",");

}

The output result: fitst, second, third

The first way: use for...in this way of traversal, for example:

?

var arr = new Array("first", "second", "third")

for(var item in arr ) {

document.write(arr[item]+",");

}

Output results: fitst, second, third


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!