Generator to array

Angelos Chalaris · JavaScript, Function, Array, Generator · Dec 31, 2020

Converts the output of a generator function to an array.

  • Use the spread operator (...) to convert the output of the generator function to an array.
const generatorToArray = gen => [...gen];
const s = new Set([1, 2, 1, 3, 1, 4]);
generatorToArray(s.entries()); // [[ 1, 1 ], [ 2, 2 ], [ 3, 3 ], [ 4, 4 ]]

Recommended snippets & collections

  • JavaScript generator functions are a more advanced yet very powerful JavaScript ES6 feature, which you can start using in your code right now.

  • Chunk iterable

    JavaScript, Function

    Chunks an iterable into smaller arrays of a specified size.

  • Cycle generator

    JavaScript, Function

    Creates a generator, looping over the given array indefinitely.

  • Convert function to variadic

    JavaScript, Function

    Changes a function that accepts an array into a variadic function.