js循环遍历的两种方法for循环和for ... in 循环

时间:2020-12-21 02:43:02
JS数组的遍历方法有两种:
第一种:一般的for循环,例如:
var a = new Array("first", "second", "third")
for(var i = 0;i < a.length; i++) {
document.write(a[i]+",");
}
输出的结果:fitst,second,third 第二种:用for...in 这种遍历的方式,例如:
var arr = new Array("first", "second", "third")
for(var item in arr) {
document.write(arr[item]+",");
}
输出的结果:fitst,second,third