7.0 for-of: Traversing complex data types 7.0.1 es5--for-in traversing arrays
Used primarily to traverse JSON objects
let arr = [‘a‘,‘b‘,‘c‘,‘d‘];for(let i in arr){ console.log(arr[i]);}//注释:for(创建一个变量);in(in要遍历的复杂数据类型)
7.0.2 es6--for-of
遍历数组:let arr = [‘a‘,‘b‘,‘c‘,‘d‘];for(let item of arr){ console.log(arr[item]);}//注释:for(创建一个变量);of(of要遍历的复杂数据类型)遍历集合:let s = new Set [‘a‘,‘b‘,‘c‘,‘d‘];for(let item of s){ console.log(arr[item]);}遍历字符串:let str = ‘hello world‘;for(let item of str){ console.log(arr[item]);}遍历映射:let m = new Map[[‘a‘,‘b‘],[‘c‘,‘d‘]];for(let item of m){ console.log(arr[item]);}let m = new Map[[‘a‘,‘b‘],[‘c‘,‘d‘]];for(let [key,value] of m){ console.log(key,value);}
7.0.3 Extension: The Walker generation function (used with for-of). Keys (): Traversal key. VALUES (): Iterates over values, not for arrays. Entries (): Traversing Keys and values
let arr = [‘a‘,‘b‘,‘c‘,‘d‘];for(let item of arr.keys()){ console.log(item);}let arr = [‘a‘,‘b‘,‘c‘,‘d‘];for(let item of arr.values()){ console.log(item);}let arr = [‘a‘,‘b‘,‘c‘,‘d‘];for(let item of arr.entries()){ console.log(item);}let arr = [‘a‘,‘b‘,‘c‘,‘d‘];for(let [k,v] of arr.entries()){ console.log(k,v);}
Iterating through an array