Laravel  
laravel
文档
数据库
架构
入门
php技术
    
Laravelphp
laravel / php / java / vue / mysql / linux / python / javascript / html / css / c++ / c#

js set 转数组

作者:迷失未来   发布日期:2025-10-24   浏览:84

// 将 Set 转换为数组的方法

// 创建一个 Set 对象
const mySet = new Set([1, 2, 3, 4, 5]);

// 方法一:使用扩展运算符 ...
const arrayFromSet1 = [...mySet];
console.log(arrayFromSet1); // 输出: [1, 2, 3, 4, 5]

// 方法二:使用 Array.from() 方法
const arrayFromSet2 = Array.from(mySet);
console.log(arrayFromSet2); // 输出: [1, 2, 3, 4, 5]

解释说明:

  1. 创建 Set 对象:我们首先创建了一个包含 1 到 5 的 Set 对象。
  2. 方法一:使用扩展运算符 (...) 可以将 Set 中的元素展开并放入一个新的数组中。
  3. 方法二Array.from() 是一个内置方法,它可以将类数组对象或可迭代对象(如 Set)转换为数组。

这两种方法都可以有效地将 Set 转换为数组。

上一篇:js array 方法

下一篇:js 对象数组去重

大家都在看

js 数组对象排序

js 数组删掉第一个值

js fill

js 数组连接

js json数组

js 数组复制

js 复制数组

js 数组拷贝

js 对象数组合并

js 对象转数组

Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3

Laravel 中文站