Home >Web Front-end >JS Tutorial >js遍历数组的两种方法

js遍历数组的两种方法

WBOY
WBOYOriginal
2016-06-01 09:55:061500browse

JS数组的遍历方法有两种:

第一种:一般的for循环,例如:

<code>var a = new Array("first", "second", "third") 
for(var i = 0;i </code>

输出的结果:fitst,second,third

 

第二种:用for...in 这种遍历的方式,例如:

<code>var arr = new Array("first", "second", "third") 
for(var item in arr) {
   document.write(arr[item]+",");
}</code>

输出的结果:fitst,second,third

 

Statement:
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