Convert ES6 Iterable to Array

前端 未结 6 1939
走了就别回头了
走了就别回头了 2020-11-28 09:30

Say you have an array-like Javascript ES6 Iterable that you know in advance will be finite in length, what\'s the best way to convert that to a Javascript Array?

The

6条回答
  •  不知归路
    2020-11-28 10:07

    You can use the Array.from method, which is being added in ES6, but only supports arrays and iterable objects like Maps and Sets (also coming in ES6). For regular objects, you can use Underscore's toArray method or lodash's toArray method, since both libraries actually have great support for objects, not just arrays. If you are already using underscore or lodash, then luckily they can handle the problem for you, alongside adding various functional concepts like map and reduce for your objects.

提交回复
热议问题