javascript怎么将set转为数组

javascript怎么将set转为数组,第1张

javascript怎么将set转为数组

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

本教程 *** 作环境: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);

方法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高级教程】

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

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/693672.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-22
下一篇 2022-04-22

发表评论

登录后才能评论

评论列表(0条)

保存