for...of
Baseline
Widely available
This feature is well established and works across many devices and browser versions. Itâs been available across browsers since 2015ë 7ì.
for...of ëª
ë ¹ë¬¸ì ë°ë³µê°ë¥í ê°ì²´ (Array, Map, Set, String, TypedArray, arguments ê°ì²´ ë±ì í¬í¨)ì ëí´ì ë°ë³µíê³ ê° ê°ë³ ìì±ê°ì ëí´ ì¤íëë ë¬¸ì´ ìë ì¬ì©ì ì ì ë°ë³µ íí¬ë¥¼ í¸ì¶íë 루í를 ìì±í©ëë¤.
ìëí´ ë³´ê¸°
const array1 = ["a", "b", "c"];
for (const element of array1) {
console.log(element);
}
// Expected output: "a"
// Expected output: "b"
// Expected output: "c"
구문
for (variable of iterable) {
statement;
}
ìì
>Arrayì ëí´ ë°ë³µ
let iterable = [10, 20, 30];
for (let value of iterable) {
console.log(value);
}
// 10
// 20
// 30
let ëì constë ì¬ì©í ì ììµëë¤, ë¸ë¡ ë´ë¶ ë³ì를 ìì íì§ ìë ê²½ì°.
let iterable = [10, 20, 30];
for (const value of iterable) {
console.log(value);
}
// 10
// 20
// 30
Stringì ëí´ ë°ë³µ
let iterable = "boo";
for (let value of iterable) {
console.log(value);
}
// "b"
// "o"
// "o"
TypedArrayì ëí´ ë°ë³µ
let iterable = new Uint8Array([0x00, 0xff]);
for (let value of iterable) {
console.log(value);
}
// 0
// 255
Mapì ëí´ ë°ë³µ
let iterable = new Map([
["a", 1],
["b", 2],
["c", 3],
]);
for (let entry of iterable) {
console.log(entry);
}
// [a, 1]
// [b, 2]
// [c, 3]
for (let [key, value] of iterable) {
console.log(value);
}
// 1
// 2
// 3
Setì ëí´ ë°ë³µ
let iterable = new Set([1, 1, 2, 2, 3, 3]);
for (let value of iterable) {
console.log(value);
}
// 1
// 2
// 3
DOM 컬ë ì ì ëí´ ë°ë³µ
NodeList ê°ì DOM 컬ë ì
ì ëí´ ë°ë³µ: ë¤ì ìë articleì ì§ê³ ììì¸ paragraphì read í´ëì¤ë¥¼ ì¶ê°í©ëë¤:
// 주ì: ì´ë NodeList.prototype[Symbol.iterator]ê°
// 구íë íë«í¼ììë§ ìëí©ëë¤
let articleParagraphs = document.querySelectorAll("article > p");
for (let paragraph of articleParagraphs) {
paragraph.classList.add("read");
}
ìì±ê¸°ì ëí´ ë°ë³µ
ìì±ê¸°ì ëí´ìë ë°ë³µí ì ììµëë¤:
function* fibonacci() {
// ìì±ê¸° í¨ì
let [prev, curr] = [1, 1];
while (true) {
[prev, curr] = [curr, prev + curr];
yield curr;
}
}
for (let n of fibonacci()) {
console.log(n);
// 1000ìì ìì´ì ìë¦
if (n >= 1000) {
break;
}
}
ë¤ë¥¸ ë°ë³µê°ë¥ ê°ì²´ì ëí´ ë°ë³µ
iterable íë¡í ì½ì ëª ìí´ì 구ííë ê°ì²´ì ëí´ìë ë°ë³µí ì ììµëë¤:
var iterable = {
[Symbol.iterator]() {
return {
i: 0,
next() {
if (this.i < 3) {
return { value: this.i++, done: false };
}
return { value: undefined, done: true };
},
};
},
};
for (var value of iterable) {
console.log(value);
}
// 0
// 1
// 2
for...ofì for...inì ì°¨ì´
for...in 루íë ê°ì²´ì 모ë ì´ê±°ê°ë¥í ìì±ì ëí´ ë°ë³µí©ëë¤.
for...of 구문ì 컬ë ì
ì ì©ì
ëë¤. 모ë ê°ì²´ë³´ë¤ë, [Symbol.iterator] ìì±ì´ ìë 모ë 컬ë ì
ììì ëí´ ì´ ë°©ìì¼ë¡ ë°ë³µí©ëë¤.
ë¤ì ìë for...of 루íì for...in 루íì ì°¨ì´ë¥¼ ë³´ì
ëë¤.
Object.prototype.objCustom = function () {};
Array.prototype.arrCustom = function () {};
let iterable = [3, 5, 7];
iterable.foo = "hello";
for (let i in iterable) {
console.log(i); // logs 0, 1, 2, "foo", "arrCustom", "objCustom"
}
for (let i of iterable) {
console.log(i); // logs 3, 5, 7
}
ëª ì¸ì
| Specification |
|---|
| ECMAScript® 2027 Language Specification> # sec-for-in-and-for-of-statements> |
ë¸ë¼ì°ì í¸íì±
ê°ì´ 보기
- for each...in - ë¹ì·í 문ì´ì§ë§, ìì± ì´ë¦ ìì²´ë³´ë¤ë ê°ì²´ì ìì±ê°ì ë°ë³µí©ëë¤ (ì¬ë¼ì§).
Array.prototype.forEach()- Map.prototype.forEach()