javascript如何将set转为数组

本篇内容主要讲解“javascript如何将set转为数组”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“javascript如何将set转为数组”吧!

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如何将set转为数组”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!