Js
js 数组遍历
使用forEach遍历
var arr=[1,2,3,4];
arr.forEach(function(val, index) {
console.log(val, index);
});
//1 0
//2 1
//3 2
//4 3
使用for..in..遍历
var arr=["张三","李四","王五","赵六"];
for (var i in arr){
console.log(i,":",arr[i]);
}
//0:张三
//1:李四
//2:王五
//3:赵六
使用for-of遍历
不仅支持数组,还支持大多数类数组对象,例如DOM NodeList对象.也支持字符串遍历,它将字符串视为一系列的Unicode字符来进行遍历.
var arr=["张三","李四","王五","赵六"];
for (var value of arr){
console.log(value);
}
//张三
//李四
//王五
//赵六