JavaScript for of 语句循环遍历可迭代对象的值
它允许您循环遍历可迭代的数据结构,例如数组、字符串、映射、节点列表等:
语法
for (variable of iterable) {
// code block to be executed
}
variable - 对于每次迭代,下一个属性的值都会分配给变量。变量可以用 const、let 或 var 声明。
iterable - 具有可迭代属性的对象。
遍历数组
实例
const cars = ["BMW", "Volvo", "Mini"];
let text = "";
for (let x of cars) {
text += x;
}
亲自试一试
遍历字符串
实例
let language = "JavaScript";
let text = "";
for (let x of language) {
text += x;
}
如需转载,请注明文章出处和来源网址:http://www.divcss5.com/html/h64424.shtml