
小结:JS中forEach,for in,for of循环。
一般的遍历数组的方法:
var Array = [1,2,3,4,5,6,7]; for (var i = 0; i < array.length; i++) { console.log(i,array[i]); }
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);
} 





网友评论文明上网理性发言 已有0人参与
发表评论: