Typescript : 遍历Array的方法:for, forEach, every等

方法一,for…of 
这个貌似是最常用的方法,angular 2中HTML语法绑定也是要的这种语法。

let someArray = [1, "string", false];

for (let entry of someArray) {
    console.log(entry); // 1, "string", false
}

 

 

 for…in 
官方文档上强调了for…in和for…of的区别:

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"
}

 

 

方法三,forEach 
forEach其实是JavaScript的循环语法,TypeScript作为javascript的语法超集,当然默认也是支持的。

let list = [4, 5, 6];
list.forEach((val, idx, array) => {
    // val: 当前值
    // idx:当前index
    // array: Array
});
 

方法四,every和some 
every和some也都是JavaScript的循环语法,TypeScript作为JavaScript的语法超集,当然默认也是支持的。因为forEach在iteration中是无法返回的,所以可以使用every和some来取代forEach。

let list = [4, 5, 6];
list.every((val, idx, array) => {
    // val: 当前值
    // idx:当前index
    // array: Array
    return true; // Continues
    // Return false will quit the iteration
});

 

posted @ 2017-06-25 22:54  善未易明  阅读(4911)  评论(0编辑  收藏  举报