30秒学会 JavaScript 片段 · 2023年2月14日

30秒学会 JavaScript 片段 – Generator to array

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 ]]

翻译自:https://www.30secondsofcode.org/js/s/generator-to-array