javascript怎么将set转为数组

文章2022-10-01125 人已阅来源:网络

javascript将set转为数组的方法:1、利用扩展运算符“...”,语法“array = [...setObject];”;2、使用“Array.from()”方法,语法“Array.from(setObject)”。

javascript怎么将set转为数组

本教程操作环境:windows7系统、javascript1.8.5版、Dell G3电脑。

javascript将set(集合)转为数组的方法:

方法1:使用扩展运算符“...

语法:var variablename = [...value];

示例:

const set =  new Set(['hello', 'world', '!']); 
console.log(set);
const array = [...set]; 
console.log(array);

javascript怎么将set转为数组

方法2:使用Array.from()方法

Array.from()方法从对象或可迭代对象(如Map,Set等)返回一个新数组。

语法:Array.from(arrayLike object);

示例:

const set =  new Set(['welcome',  'you','!']); 
console.log(set);
console.log(Array.from(set))

javascript怎么将set转为数组

【推荐学习:javascript高级教程】

以上就是javascript怎么将set转为数组的详细内容!