Collect yielded values into an array.

iterator-to-array `Array.from` can consume an iterable and store its yielded values. After that, array methods can summarize the collected data.

Iterator To Array

base
iterator_to_array.js
Replay: real traced execution (multi-file project)
const base = 2;

function* multiples(start) {
  yield start;
  yield start * 2;
  yield start * 3;
}

const values = Array.from(multiples(base));
const summary = values.map((value) => value + "x").join(",");

console.log("base=" + base);
console.log("summary=" + summary);
const base = 1;

function* multiples(start) {
  yield start;
  yield start * 2;
  yield start * 3;
}

const values = Array.from(multiples(base));
const summary = values.map((value) => value + "x").join(",");

console.log("base=" + base);
console.log("summary=" + summary);
const base = 4;

function* multiples(start) {
  yield start;
  yield start * 2;
  yield start * 3;
}

const values = Array.from(multiples(base));
const summary = values.map((value) => value + "x").join(",");

console.log("base=" + base);
console.log("summary=" + summary);
  1. base ← 2

    1const base→ 2 = 2; //@base=1, 423function* multiples(start) {4  yield start;5  yield start * 2;6  yield start * 3;7}89const values = Array.from(multiples(base2));10const summary = values.map((value) => value + "x").join(",");
  2. start ← 2

    1const base = 2; //@base=1, 423function* multiples(start2) {4  yield start2;5  yield start→ 2 * 2;6  yield start→ 2 * 3;7}
  3. values ← 2,4,6, summary ← 2x,4x,6x

    6  yield start * 3;7}89const values→ 2,4,6 = Array.from(multiples(base2));10const summary→ 2x,4x,6x = values2,4,6.map((value) => value + "x").join(",");1112console.log("base=" + base2);13console.log("summary=" + summary2x,4x,6x);
    outputbase=2
    summary=2x,4x,6x
  1. base ← 1

    1const base→ 1 = 1;23function* multiples(start) {4  yield start;5  yield start * 2;6  yield start * 3;7}89const values = Array.from(multiples(base1));10const summary = values.map((value) => value + "x").join(",");
  2. start ← 1

    1const base = 1;23function* multiples(start1) {4  yield start1;5  yield start→ 1 * 2;6  yield start→ 1 * 3;7}
  3. values ← 1,2,3, summary ← 1x,2x,3x

    6  yield start * 3;7}89const values→ 1,2,3 = Array.from(multiples(base1));10const summary→ 1x,2x,3x = values1,2,3.map((value) => value + "x").join(",");1112console.log("base=" + base1);13console.log("summary=" + summary1x,2x,3x);
    outputbase=1
    summary=1x,2x,3x
  1. base ← 4

    1const base→ 4 = 4;23function* multiples(start) {4  yield start;5  yield start * 2;6  yield start * 3;7}89const values = Array.from(multiples(base4));10const summary = values.map((value) => value + "x").join(",");
  2. start ← 4

    1const base = 4;23function* multiples(start4) {4  yield start4;5  yield start→ 4 * 2;6  yield start→ 4 * 3;7}
  3. values ← 4,8,12, summary ← 4x,8x,12x

    6  yield start * 3;7}89const values→ 4,8,12 = Array.from(multiples(base4));10const summary→ 4x,8x,12x = values4,8,12.map((value) => value + "x").join(",");1112console.log("base=" + base4);13console.log("summary=" + summary4x,8x,12x);
    outputbase=4
    summary=4x,8x,12x