JS数组的遍历方式for循环与for...in

阅读更多

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

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

?
1
2
3
4
var a = new Array( "first" , "second" , "third" )
for ( var i = 0;i < a.length; i++) {
document.write(a[i]+ "," );
}

输出的结果:fitst,second,third

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

?
1
2
3
4
var arr = new Array( "first" , "second" , "third" )
for ( var item in arr) {
document.write(arr[item]+ "," );
}

输出的结果:fitst,second,third

你可能感兴趣的:(JS遍历,JS)