DEV Community

Cover image for Convert Iterable to Array using Spread

Convert Iterable to Array using Spread

Samantha Ming on November 11, 2019

Convert Iterables to Array using Spread Use ES6 spread (...) to easily convert Iterables into an Array! Often, iterables are limited i...
Collapse
 
nickytonline profile image
Nick Taylor

I like using spread as well, but if you want to e.g. convert a NodeList to an array and then map over it, consider using Array.from.

I discovered this summer that Array.from has a second argument which is a map callback function that is called as each item is created.

Looking forward to your next post!

Collapse
 
samanthaming profile image
Samantha Ming

That totally is the benefit with Array.from! Awesome point, let me add it to the code notes😁👍

Collapse
 
mateiadrielrafael profile image
Matei Adriel

Whoa, thats awesome

Collapse
 
mateiadrielrafael profile image
Matei Adriel • Edited

Cant wait to be able to do

const set = new Set([1, 2, 3])
const array = set |> Array.from
Collapse
 
samanthaming profile image
Samantha Ming

That’s new syntax for me🤩...what language is that based from? 😮

Collapse
 
mateiadrielrafael profile image
Matei Adriel

Probably haskell or some other functional lamg