1、for循环
for(let i=0;i<5;i++){
console.log("i= ",i);
}
2、for in循环
var person={fname:"Bill",lname:"Gates",age:56}; var txt="";
for (let x in person) {
txt=txt + person[x];
}
console.log("txt= ",txt);
//txt= BillGates56
3、foreach 循环
array.forEach(function(currentValue, index, arr))
currentValue:当前元素(必须) index:当前元素索引 arr:当前元素所属数组
let arr=[{name:"dlj",sex:"gril"},{name:"dlj1",sex:"gril"}];
arr.forEach(function(item,index){
console.log(index," is ",item.name);
})
//0 is dlj
//1 is dlj1
4、 for of循环
var person=["bill","gates"]; var txt="";
for (let x of person) {
txt=txt + x;
}
console.log("txt= ",txt);
//txt= billgates
备注:
for in 与for of 区别:
a. for in 便历出来的是属性
b. for of 遍历的是value
c. 手动给对象添加属性后, for in 是可以将新添加的属性遍历出来 但是for of 不行,
d. for in 的属性是使用[] 不可以使用 "." eg: data['index'] instead of data.index