一、一般的遍历数组的方法:
var array = [1,2,3,4,5,6,7];
for (var i = 0; i < array.length; i++) {
console.log(i,array[i]);
}
|
二、用for in的方遍历数组,得到的是索引
var array = [1,2,3,4,5,6,7];
for(let index in array) {
console.log(index,array[index]);
};
|
三、forEach,得到的是元素
var array = [1,2,3,4,5,6,7];
array.forEach(e=>{
console.log(e);
});
array.forEach(function(e){
console.log(e);
});
|
四、用for in不仅可以对数组,也可以对enumerable对象操作!得到的是索引
var table = {
a : 10,
b : true,
c : "jadeshu"
};
for(let index in table) {
console.log(index, table[index]);
}
|
五、在ES6中,增加了一个for of循环,得到的是元素
var array = [1,2,3,4,5,6,7];
for(let ele of array) {
console.log(ele);
};
var str = "helloabc";
for(let ele of str) {
console.log(ele);
}
|
for of不能对象用