From 17c9cf690b3f33caf77ccbc321d9f306dff332a6 Mon Sep 17 00:00:00 2001 From: Chalarangelo Date: Thu, 31 Dec 2020 13:22:18 +0200 Subject: [PATCH] Add generatorToArray --- snippets/generatorToArray.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 snippets/generatorToArray.md diff --git a/snippets/generatorToArray.md b/snippets/generatorToArray.md new file mode 100644 index 000000000..bdddeda1e --- /dev/null +++ b/snippets/generatorToArray.md @@ -0,0 +1,17 @@ +--- +title: generatorToArray +tags: function,array,generator,beginner +--- + +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. + +```js +const generatorToArray = gen => [...gen]; +``` + +```js +const s = new Set([1, 2, 1, 3, 1, 4]); +generatorToArray(s.entries()); // [[ 1, 1 ], [ 2, 2 ], [ 3, 3 ], [ 4, 4 ]] +```