in javascript, create Array from an iterator -
this question has answer here:
- transforming javascript iterator array 3 answers
given iterator
, what's best way create array
?
for example,
let map = new map(); map.set( 'key1', 'data' ); map.set( 'key2', 'more data' ); ... // now, wish have array of keys let arr = //??// map.keys() //??//
i lame like
function iter2array( iter ) { let arr = new array(); for( let e in iter ) arr.push(e); return arr; }
but there has better way.
array.from(map.keys()) // ['key1', 'key2']
the array.from() method creates new array instance array-like or iterable object.
Comments
Post a Comment