This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

Iterator.prototype.take()

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 ์ธ์Šคํ„ด์Šค์˜ take() ๋ฉ”์„œ๋“œ๋Š” ์ด ๋ฐ˜๋ณต์ž์—์„œ ์ฃผ์–ด์ง„ ์ˆซ์ž๋งŒํผ์˜ ์š”์†Œ๋ฅผ ์‚ฐ์ถœํ•˜๊ณ  ์ข…๋ฃŒํ•˜๋Š” ์ƒˆ๋กœ์šด ๋ฐ˜๋ณต์ž ํ—ฌํผ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

๊ตฌ๋ฌธ

js
take(limit)

๋งค๊ฐœ๋ณ€์ˆ˜

limit

์ˆœํšŒ ์‹œ์ž‘์ ๋ถ€ํ„ฐ ์ทจํ•  ์š”์†Œ์˜ ์ˆ˜.

๋ฐ˜ํ™˜ ๊ฐ’

์ƒˆ๋กœ์šด ๋ฐ˜๋ณต์ž ํ—ฌํผ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๋ฐ˜ํ™˜๋œ ๋ฐ˜๋ณต์ž ํ—ฌํผ๋Š” ์›๋ž˜ ๋ฐ˜๋ณต์ž์˜ ์š”์†Œ๋“ค์„ ํ•˜๋‚˜์”ฉ ์‚ฐ์ถœํ•˜๊ณ , limit ๊ฐœ์ˆ˜๋งŒํผ์˜ ์š”์†Œ๊ฐ€ ์‚ฐ์ถœ๋˜์—ˆ๊ฑฐ๋‚˜ ์›๋ž˜ ๋ฐ˜๋ณต์ž๊ฐ€ ์†Œ์ง„๋˜์—ˆ์„ ๋•Œ, ๋‘˜ ์ค‘ ๋จผ์ € ๋ฐœ์ƒํ•˜๋Š” ๊ฒฝ์šฐ ์™„๋ฃŒ๋ฉ๋‹ˆ๋‹ค (next() ๋ฉ”์„œ๋“œ๊ฐ€ { value: undefined, done: true }๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค).

์˜ˆ์™ธ

RangeError

์ •์ˆ˜๋กœ ๋ณ€ํ™˜์‹œ limit๊ฐ€ NaN ๋˜๊ฑฐ๋‚˜ ์Œ์ˆ˜์ผ ๊ฒฝ์šฐ์— ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

์˜ˆ์ œ

take() ์‚ฌ์šฉํ•˜๊ธฐ

๋‹ค์Œ ์˜ˆ์ œ๋Š” ํ”ผ๋ณด๋‚˜์น˜ ์ˆ˜์—ด์˜ ํ•ญ์„ ์ƒ์„ฑํ•˜๋Š” ๋ฐ˜๋ณต์ž๋ฅผ ๋“ค๊ณ , ์ฒ˜์Œ ์„ธ ํ•ญ์„ ์ถœ๋ ฅํ•ฉ๋‹ˆ๋‹ค.

js
function* fibonacci() {
  let current = 1;
  let next = 1;
  while (true) {
    yield current;
    [current, next] = [next, current + next];
  }
}

const seq = fibonacci().take(3);
console.log(seq.next().value); // 1
console.log(seq.next().value); // 1
console.log(seq.next().value); // 2
console.log(seq.next().value); // undefined

for...of ๋ฃจํ”„์™€ ๊ฐ™์ด take() ์‚ฌ์šฉํ•˜๊ธฐ

take()์€ ๋ฐ˜๋ณต์ž๋ฅผ ์ง์ ‘ ์ด๋™์‹œํ‚ค์ง€ ์•Š์„ ๋•Œ ๊ฐ€์žฅ ํŽธ๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ๋ฐ˜๋ณต์ž๋„ ์ˆœํšŒ ๊ฐ€๋Šฅํ•˜๊ธฐ ๋•Œ๋ฌธ์—, ๋ฐ˜ํ™˜๋œ ํ—ฌํผ๋ฅผ for...of ๋ฃจํ”„๋กœ ๋ฐ˜๋ณตํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

js
for (const n of fibonacci().take(5)) {
  console.log(n);
}

// Logs:
// 1
// 1
// 2
// 3
// 5

fibonacci()๋Š” ๋ฌดํ•œ ๋ฐ˜๋ณต์ž์ด๊ธฐ ๋•Œ๋ฌธ์— ์ง์ ‘์ ์œผ๋กœ ์ˆœํšŒ์‹œํ‚ค๊ธฐ ์œ„ํ•ด for ๋ฃจํ”„๋ฅผ ์‚ฌ์šฉํ•  ํ•„์š”๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.

drop()๊ณผ take() ๊ฒฐํ•ฉํ•˜๊ธฐ

๋ฐ˜๋ณต์ž์˜ ์ผ๋ถ€๋ฅผ ์–ป๊ธฐ ์œ„ํ•ด take()๊ณผ Iterator.prototype.drop()์„ ๊ฐ™์ด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

js
for (const n of fibonacci().drop(2).take(5)) {
  // ์ฒซ ๋‘ ๊ฐœ์˜ ์š”์†Œ๋ฅผ ๋ฒ„๋ฆฌ๊ณ , ๋‹ค์Œ 5๊ฐœ๋ฅผ ์ทจํ•ฉ๋‹ˆ๋‹ค
  console.log(n);
}
// ๋กœ๊ทธ:
// 2
// 3
// 5
// 8
// 13

for (const n of fibonacci().take(5).drop(2)) {
  // ์ฒ˜์Œ 5๊ฐœ๋ฅผ ์ทจํ•˜๊ณ , ์ฒ˜์Œ 2๊ฐœ๋ฅผ ๋ฒ„๋ฆฝ๋‹ˆ๋‹ค
  console.log(n);
}
// ๋กœ๊ทธ:
// 2
// 3
// 5

take ๊ฐฏ์ˆ˜์˜ ์ƒ๋‹จ๊ณผ ํ•˜๋‹จ

limit๊ฐ€ ์Œ์ˆ˜๊ฑฐ๋‚˜ NaN์ผ ๊ฒฝ์šฐ RangeError๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

js
fibonacci().take(-1); // RangeError: -1 must be positive
fibonacci().take(undefined); // RangeError: undefined must be positive

limit๊ฐ€ ๋ฐ˜๋ณต์ž๊ฐ€ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋Š” ์ด ์š”์†Œ ์ˆ˜๋ณด๋‹ค ํฐ ๊ฒฝ์šฐ(Infinity์™€ ๊ฐ™์ด), ๋ฐ˜ํ™˜๋œ ๋ฐ˜๋ณต์ž ํ—ฌํผ๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ์›๋ž˜ ๋ฐ˜๋ณต์ž์™€ ๊ฐ™์ด ํ–‰๋™ํ•ฉ๋‹ˆ๋‹ค.

js
for (const n of new Set([1, 2, 3]).values().take(Infinity)) {
  console.log(n);
}

// ๋กœ๊ทธ:
// 1
// 2
// 3

๋ช…์„ธ์„œ

Specification
ECMAScriptยฎ 2026 Language Specification
# sec-iterator.prototype.take

๋ธŒ๋ผ์šฐ์ € ํ˜ธํ™˜์„ฑ

๊ฐ™์ด ๋ณด๊ธฐ