[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Reference/Global_Objects/Iterator/map [Back]  [Original]

Iterator.prototype.map() - JavaScript | MDN

MDN Web Docs

View in English Always switch to English

Iterator.prototype.map()

Baseline 2025

20253

map() Iterator

js
map(callbackFn)

callbackFn

element

index

next() callbackFn next() { value: undefined, done: true }

map()

map()

2

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

const seq = fibonacci().map((x) => x ** 2);
console.log(seq.next().value); // 1
console.log(seq.next().value); // 1
console.log(seq.next().value); // 4

map() for...of

map() for...of

js
for (const n of fibonacci().map((x) => x ** 2)) {
  console.log(n);
  if (n > 30) {
    break;
  }
}

// Logs:
// 1
// 1
// 4
// 9
// 25
// 64

This is equivalent to:

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

ECMAScript 2027 LanguageSpecification
# sec-iterator.prototype.map


Web Proxy Viewer  |  New URL  |  Original Page