41

I want to create an array from the values of an generator in JavaScript. The generator creates a sequence of dynamic length like this

function* sequenceGenerator(minVal, maxVal) {
    let currVal = minVal;

    while(currVal < maxVal)
        yield currVal++;
}

I want to store those values in an array but using next() until the generator is done does not seem to be the best way possible (and looks quite ugly to be honest).

var it, curr, arr;

it = sequenceGenerator(100, 1000);
curr = it.next();
arr = [];

while(! curr.done){
    arr.push(curr.value);
}

Can I somehow create an array directly from/within the generator? If not, can I somehow avoid/hide the loop? Maybe by using map or something like that?

Thanks in advance.

5 Answers 5

66

One short solution might be:

let list = [...sequenceGenerator(min, max)]

Documentation on MDN

Sign up to request clarification or add additional context in comments.

Comments

39

I found another way

var arr = Array.from( sequenceGenerator(min, max) );

works aswell.

1 Comment

Interesting, why was this accepted instead of simpler one and the one, that had more votes. I thought, that idea, is to mark best answer as the accepted one, not just provde own and accept it.
4

You can do it like this;

function* sequenceGenerator() {
  let currVal = this.minVal;
  while(currVal <= this.maxVal) yield currVal++;
}
var obj = {minVal: 10, maxVal:20},
    arr;
obj[Symbol.iterator] = sequenceGenerator;
arr = [...obj];
console.log(arr);

Comments

0

And yet another way (since ECMAScript 2025):

const arr = sequenceGenerator(min, max).toArray();

function* sequenceGenerator(start, stop) {
    for (let val = start; val < stop; val++) yield val;
}

console.log(sequenceGenerator(2,5).toArray()); // [2, 3, 4]

Comments

-1
{let
  isGen = (genProto => (val => genProto.isPrototypeOf(val)))(
    Object.getPrototypeOf(function*(){}) ),
  iterMthdGen = (() => (function() { return this(); })),
  createAddIterMthdFn = (iterMthdFn => ((obj) =>
    Object.assign(obj, {[Symbol.iterator]: iterMthdFn(), }) )),
  addIterMthdGen = createAddIterMthdFn(iterMthdGen),
  wrapGen = (gen => ({[Symbol.iterator]: gen}))
  g = function*() { for(i = 0; i < 10; i++) yield i; };
// [0, 1, 2, 3, 4, 5, 6, 7, 8 , 9],  [0, 1, 2, 3, 4, 5, 6, 7, 8 , 9]
console.log([...wrapGen(g)], [...addIterMthdGen(g)]); }

Using this recipes to work with generators. wrapGen is simpliest method. addIterMthdGen makes generator also an iterable.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.