given an iterator, what's the best way to create an Array?
for example,
let map = new Map(); map.set( 'key1', 'data' ); map.set( 'key2', 'more data' ); ... // now, wish to have an array of keys let arr = //??// map.keys() //??//
I could do something lame like
function iter2array( iter ) {
let arr = new Array();
for( let e in iter ) arr.push(e);
return arr;
}
but there has to be a better way.
Array.from(map.keys()) // ['key1', 'key2']
The Array.from() method creates a new Array instance from an array-like or iterable object.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With