跳到主要内容

for...of 循环

1. 遍历数组

  • for...of 循环可以遍历数组中的每个元素
let array = [1, 2, 3];
for (let value of array) {
logd(value); // 输出: 1, 2, 3
}

2. 遍历字符串

  • for...of 循环可以遍历字符串中的每个字符
let string = "hello";
for (let char of string) {
logd(char); // 输出: h, e, l, l, o
}

3. 遍历包含辅助平面字符的字符串

  • for...of 循环可以正确处理包含辅助平面字符(如表情符号)的字符串
let astralString = "😀🎉";
for (let char of astralString) {
logd(char); // 输出: 😀, 🎉
}