本文介绍了将ES6 Iterable转换为Array的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设你有一个类似数组的Javascript ES6 Iterable,你提前知道的长度是有限的,把它转换成Javascript数组的最好方法是什么?
这样做的原因是许多js库,如下划线和lodash只支持数组,所以如果你想在Iterable上使用任何他们的功能,它必须首先被转换成一个数组。
在python中,您只需使用list()函数即可。在ES6中有一个等价物吗?
解决方案您可以使用 Array.from 或 spread operator 。
示例:
let s = new Set(); s.add(1); s.add(2); s.add(3); s.add(4); let a = Array.from(s); // = [1,2,3,4] let b = [... s]; // = [1,2,3,4]
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 reason for doing so is that many js libraries such as underscore and lodash only support Arrays, so if you wish to use any of their functions on an Iterable, it must first be converted to an Array.
In python you can just use the list() function. Is there an equivalent in ES6?
解决方案You can use Array.from or the spread operator.
Example:
let s = new Set(); s.add(1); s.add(2); s.add(3); s.add(4); let a = Array.from(s); // = [ 1, 2, 3, 4 ] let b = [...s]; // = [ 1, 2, 3, 4 ]
更多推荐
将ES6 Iterable转换为Array
发布评论