正在加载文章
for...of 语句创建一个循环来迭代可迭代的对象。在 ES6 中引入的 for...of 循环,以替代 for...in 和 forEach() ,并支持新的迭代协议。for...of 允许你遍历 Arrays(数组), Strings(字符串), Maps(映射), Sets(集合)等可迭代的数据结构等。
let arr = [2,'aa','bb']
let str = 'hello'
let ma = new Map([["a",1],["b",2],["c",3]])
let se = new Set([1,2,3,2,3,2,1])
let obj = {name: 'tom', age: 18}
for (const i of arr) {
console.log(i)
}
// 2
// aa
// bb
for (const i of str) {
console.log(i)
}
// h
// e
// l
// l
// o
for (const i of ma) {
console.log(i)
}
// [ 'a', 1 ]
// [ 'b', 2 ]
// [ 'c', 3 ]
for (const i of se) {
console.log(i)
}
// 1
// 2
// 3
for (const i of obj) {
console.log(i)
}
// TypeError: obj is not iterable
let arr = [2,'aa','bb']
let str = 'hello'
let ma = new Map([["a",1],["b",2],["c",3]])
let se = new Set([1,2,3,2,3,2,1])
let obj = {name: 'tom', age: 18}
for (const i in arr) {
console.log(i)
}
// 0
// 1
// 2
for (const i in str) {
console.log(i)
}
// 0
// 1
// 2
// 3
// 4
for (const i in ma) {
console.log(i)
}
// error
for (const i of se) {
console.log(i)
}
// error
for (const i of obj) {
console.log(i)
}
// name
// age
for...of 能遍历数组、字符串、映射、集合类型,但不能遍历对象;for...in 只能遍历数组、字符串、对象for...of 可以获取可迭代对象的每个值,for...in 获取索引for...in 参考:https://www.cnblogs.com/m2maomao/p/7743143.html
11 JS 中的单线程和事件循环 参考:https://blog.csdn.net/qq_36995542/article/details/80007381 09 node.js 学习之路(五) CRUDBootStrap 下载模板https://v3.bootcss.com/examples/dashboard/ 从 JSON 中读取数据由于通过 fs.readFile 读取的 dat...