【JavaScript】将 Set 集合 A 转换为数组
方法汇总
1. Array.from() 方法
如果有一个集合(Set)对象,可以使用Array.from()
方法来将集合转换为数组,这个方法创建一个新的数组实例,从一个可迭代的对象中复制元素。
let setA = new Set([1, 2, 3, 4, 5]);
let arrayA = Array.from(setA);
console.log(arrayA); // [1, 2, 3, 4, 5]
2. 扩展操作符 ...
使用扩展操作符...
也可以将 Set 集合转换为数组
let setA = new Set([1, 2, 3, 4, 5]);
let arrayA = [...setA];
console.log(arrayA); // [1, 2, 3, 4, 5]