主要用来遍历数组和字符串,对象也可以
for...in方法针对的是有没有下标数组的情况,也适用于对象的循环遍历;
for...of返回下标, 适用有下标的数组,对象没有下标则不适用。
一.for...in的用法,返回下标
var list = ["a", "b", "c", "d", "e", "f"];
console.log(list);
for(var index in list){
console.log("for_in的下标:",index);
}
(6) ["a", "b", "c", "d", "e", "f"]
01 for..in(of).html:54 for_in的下标: 0
01 for..in(of).html:54 for_in的下标: 1
01 for..in(of).html:54 for_in的下标: 2
01 for..in(of).html:54 for_in的下标: 3
01 for..in(of).html:54 for_in的下标: 4
01 for..in(of).html:54 for_in的下标: 5
二.for..of的用法,返回元素
for (var item of list) {
console.log("for_of的下标以及元素:",list.indexOf(item),item);
}
01 for..in(of).html:58 for_of的下标以及元素: 0 a
01 for..in(of).html:58 for_of的下标以及元素: 1 b
01 for..in(of).html:58 for_of的下标以及元素: 2 c
01 for..in(of).html:58 for_of的下标以及元素: 3 d
01 for..in(of).html:58 for_of的下标以及元素: 4 e
01 for..in(of).html:58 for_of的下标以及元素: 5 f
网友评论