Iterator.prototype.drop()

Limited availability

This feature is not Baseline because it does not work in some of the most widely-used browsers.

Experimental: 이 기능은 실험적인 기능입니다.
프로덕션 환경에서 사용하기 전에 브라우저 호환성 표를 주의 깊게 확인하세요.

Iterator 인스턴스의 drop() 메서드는 이 반복자의 시작점에서 주어진 숫자만큼의 요소를 지나치는 새로운 반복자 헬퍼를 반환합니다.

구문

js
drop(limit)

매개변수

limit

순회 시작점부터 버릴 요소의 수.

반환 값

새로운 반복자 헬퍼를 반환합니다. 반환된 반복자 헬퍼의 next() 메서드가 처음 호출될 때, 현재 반복자는 즉시 limit 개의 요소만큼 전진하고, 그 다음 요소(limit+1번째 요소)를 산출합니다. 그 후 반복자 헬퍼는 남은 요소들을 하나씩 산출합니다. 만약 현재 반복자가 limit개 미만의 요소를 가지고 있다면, 새로운 반복자 헬퍼는 next()가 처음 호출될 때 즉시 완료됩니다.

예외

RangeError

정수로 변환limitNaN 되거나 음수일 경우에 발생합니다.

예제

drop() 사용하기

다음 예제는 피보나치 수열의 항을 생성하는 반복자를 만들되, 처음 두 항을 버리고 3번째 항부터 시작하는 반복자를 생성합니다.

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

const seq = fibonacci().drop(2);
console.log(seq.next().value); // 2
console.log(seq.next().value); // 3

이는 아래와 동일합니다.

js
const seq = fibonacci();
seq.next();
seq.next();

for...of 루프와 같이 drop() 사용하기

drop()은 반복자를 직접 이동시키지 않을 때 가장 편리합니다. 반복자도 순회 가능하기 때문에, 반환된 헬퍼를 for...of 루프로 반복할 수 있습니다.

js
for (const n of fibonacci().drop(2)) {
  console.log(n);
  if (n > 30) {
    break;
  }
}

// Logs:
// 2
// 3
// 5
// 8
// 13
// 21
// 34

drop()과 take() 결합하기

반복자의 일부를 얻기 위해 drop()Iterator.prototype.take()을 같이 사용할 수 있습니다.

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

drop 갯수의 상단과 하단

limit가 음수거나 NaN일 경우 RangeError가 발생합니다.

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

limit가 반복자가 생성할 수 있는 총 요소 수보다 큰 경우(예: Infinity), 반환된 반복자 헬퍼는 next()가 처음 호출될 때 즉시 모든 요소를 버리고 완료됩니다. 만약 현재 반복자가 무한한 경우, 반환된 반복자 헬퍼는 절대 완료되지 않을 것입니다.

js
fibonacci().drop(Infinity).next(); // Never ends
new Set([1, 2, 3]).values().drop(Infinity).next(); // { value: undefined, done: true }
new Set([1, 2, 3]).values().drop(4).next(); // { value: undefined, done: true }

명세서

Specification
Iterator Helpers
# sec-iteratorprototype.drop

브라우저 호환성

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
drop

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
No support
No support
See implementation notes.
Has more compatibility info.

같이 보기