Array 遍历
2020-05-08 本文已影响0人
晨曦Bai
https://www.typescriptlang.org/docs/handbook/iterators-and-generators.html
可以迭代的类型: Array , Map , Set , String,Int32Array, Uint32Array,etc.
1. for ... of
vs for ... in
for (let i in list) {} // i 是 index / key
for (let i of list) {} // i 是 value
let list = [4, 5, 6];
for (let i in list) {
console.log(i); // "0", "1", "2",
}
for (let i of list) {
console.log(i); // "4", "5", "6"
}
- Code Generation
- Targeting ES5 and ES3
当编译目标语言为 ES5 and ES3
for ... of 转换成标准的 C
only allowed on values ofArray
type
let numbers = [1,2,3];
for (let num of numbers) {
console.log(num);
}
// generated as
var numbers = [1,2,3];
for(var _i = 0; _i < numbers.length; _i++){
console.log(num);
}
- Targeting ECMAScript 2015 and higher
the compiler will generatefor ... of