Iterator.prototype.toArray()
Baseline
2025
Newly available
Since โจMarch 2025โฉ, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.
Experimental: ์ด ๊ธฐ๋ฅ์ ์คํ์ ์ธ ๊ธฐ๋ฅ์
๋๋ค.
ํ๋ก๋์
ํ๊ฒฝ์์ ์ฌ์ฉํ๊ธฐ ์ ์ ๋ธ๋ผ์ฐ์ ํธํ์ฑ ํ๋ฅผ ์ฃผ์ ๊น๊ฒ ํ์ธํ์ธ์.
Iterator
์ธ์คํด์ค์ toArray()
๋ฉ์๋๋ ํด๋น ๋ฐ๋ณต์๋ก๋ถํฐ ์ฐ์ถ๋ ์์์ ํจ๊ป ์๋ก์ด Array
์ธ์คํด์ค๋ฅผ ์์ฑํฉ๋๋ค.
๊ตฌ๋ฌธ
toArray()
๋งค๊ฐ๋ณ์
์์.
๋ฐํ ๊ฐ
์์ฑ๋ ์์๋๋ก ๋ฐ๋ณต์๋ก๋ถํฐ ์จ ๊ฐ์ฒด๋ฅผ ํฌํจํ๋ Array
์ธ์คํด์ค์
๋๋ค.
์์
toArray() ์ฌ์ฉํ๊ธฐ
iterator.toArray()
๋ ์ฌ๋ฌ ๊ฐ์ ๋ฐ๋ณต์ ํฌํผ ๋ฉ์๋๊ฐ ๊ด๋ จ๋ ๊ฒฝ์ฐ ์ฒด์ธ ์ฐ๊ฒฐ์ด ๋ ์ฝ๋ค๋ ์ ์ ์ ์ธํ๋ฉด Array.from(iterator)
๋ฐ '[...iterator]`์ ๋์ผํฉ๋๋ค. ๋ค์ ์์ ๋ ํผ๋ณด๋์น ์์ด์ ํญ์ ์ฐ์ถํ๋ ๋ฐ๋ณต์๋ฅผ ๋ง๋ค๊ณ , ์ฒ์ 10๊ฐ์ ํญ์ ๊ฐ์ ธ์ ํ์๋ฅผ ๊ฑฐ๋ฅธ ๋ค์ ๊ฒฐ๊ณผ๋ฅผ ๋ฐฐ์ด๋ก ๋ณํํ๋ ์์ ์
๋๋ค.
function* fibonacci() {
let current = 1;
let next = 1;
while (true) {
yield current;
[current, next] = [next, current + next];
}
}
const array = fibonacci()
.take(10)
.filter((x) => x % 2 === 0)
.toArray();
console.log(array); // [2, 8, 34]
์ด ์ฒ๋ฆฌ์ ๋ง์ง๋ง ๋จ๊ณ๋ก toArray()
๋ฅผ ํธ์ถํ๋ ๊ฒ์ด ์ข์ต๋๋ค. ์๋ฅผ ๋ค์ด fibonacci().take(10).toArray().filter(...)
๋ ๋ฐ๋ณต์ ํฌํผ๊ฐ ๋๋ฆฌ๊ณ ์์ ๋ฐฐ์ด์ ์์ฑํ์ง ์๊ธฐ ๋๋ฌธ์ ํจ์จ์ฑ์ด ๋จ์ด์ง๋๋ค.
๋ช ์ธ์
Specification |
---|
ECMAScriptยฎ 2026 Language Specification # sec-iterator.prototype.toarray |
๋ธ๋ผ์ฐ์ ํธํ์ฑ
Loadingโฆ