Convert ES6 Iterable to Array

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

    You can use Array.from or the spread operator.

    Example:

    let x = new Set([ 1, 2, 3, 4 ]);
    
    let y = Array.from(x);
    console.log(y); // = [ 1, 2, 3, 4 ]
    
    let z = [ ...x ];
    console.log(z); // = [ 1, 2, 3, 4 ]

提交回复
热议问题