for...of 循环用于循环访问任何可迭代的数据类型。
for...of 循环的编写方式和 for...in 循环的基本一样,只是将 in
替换为 of
,可以忽略索引。
例如:
const digits = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
for (const digit of digits) {
console.log(digit);
}
实战:
编写符合以下条件的 for...of
循环:
- 循环访问
days
数组中的每一天 - 将一天的首字母大写
- 将这一天输出到控制台中
代码应该将以下每天输出到控制台中:
Sunday
Monday
Tuesday
Wednesday
Thursday
Friday
Saturday
我的代码:
const days = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday'];for(let item of days){
item = item.substring(0,1).toUpperCase()+item.substring(1);
console.log(item);
}