Convert ES6 Iterable to Array

前端 未结 6 1930
走了就别回头了
走了就别回头了 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:22

    You could also do:

    let arr = [];
    for (let elem of gen(...)){
        arr.push(elem);
    }
    

    Or "the hard way" using ES5 + generator function (Fiddle works in current Firefox):

    var squares = function*(n){
        for (var i=0; i

    Both are approaches are certainly not recommendable however and are merely a proof-of-concept.

提交回复
热议问题