在JavaScript中,若要将一个Set集合转换为数组,可以使用以下两种方法:
Array.from()const mySet = new Set([1, 2, 3, 4, 5, 6, 7, 8, 9]);
const array = Array.from(mySet);
console.log(array); // 输出: [1, 2, 3, 4, 5, 6, 7, 8, 9]
...)const mySet = new Set([1, 2, 3, 4, 5, 6, 7, 8, 9]);
const array = [...mySet];
console.log(array); // 输出: [1, 2, 3, 4, 5, 6, 7, 8, 9]
Set对象:Set是ES6引入的数据结构,存储唯一值。示例中的集合{1,2,3,4,5,6,7,8,9}需要通过new Set()创建。Array.from()**:直接将可迭代对象(如Set)转换为数组。...):展开Set中的元素到新数组中。Array.from(obj)或Array.prototype.slice.call(obj))。Set,若原始数据格式不同,请调整初始化方式。
热门推荐:
0