JS中的迭代 -- for of 与 for in

1 for...of:

迭代的是 可迭代的对象的value值。

1.1 array数组

let arrayList = ['a', 'b', 'c'];

for(let value of arrayList) {

// value =>  'a'、 'b'、 'c'

}

1.2 string类型

let str = 'abcde';

for(let value of str) {

value => a'、 'b'、 'c'、'd'、'e'

}

1.3 可以迭代其他如: maps、 sets、 generators、 DOM节点集合、 arguments对象

2 for ... in

2.1 迭代对象的属性

let aObj = { aKey: 'aValue', bKey: 'bValue' };

for(let key in aObj) {

key => aKey  bKey

}

2.2 迭代获取数组或者string的下标

let str = 'abcdefg';

for(let key in str) {

key => 0 1 2 3 4 5 6

}

愿时光为我加冕
原文地址:https://www.cnblogs.com/taishuhanmei/p/10905523.html