写js的时候发现js和Java for循环的一点区别
java的for循环,想遍历所有元素:
String[] arr = {"a", "b", "c"};
for (String str : arr) {
System.out.println(str);
}
结果是:
JavaScript的for循环,想遍历所有元素:
let arr = ["a","b","c"]
for (let i in arr){
console.log(i)
}
结果是:
循环的是index,而不是元素,如果要循环数组里的元素:
let arr = ["a","b","c"]
for (let i in arr){
console.log(arr[i])
}
这样结果就是数组里的元素了: