// 将 Set 转换为 Array 的示例代码
// 创建一个新的 Set 对象
const mySet = new Set([1, 2, 3, 4, 5]);
// 使用 Array.from() 方法将 Set 转换为数组
const arrayFromSet = Array.from(mySet);
// 或者使用扩展运算符(...)将 Set 转换为数组
const arrayFromSetSpread = [...mySet];
console.log(arrayFromSet); // 输出: [1, 2, 3, 4, 5]
console.log(arrayFromSetSpread); // 输出: [1, 2, 3, 4, 5]
// 解释:
// 1. Array.from() 方法可以将任何可迭代对象(如 Set)转换为数组。
// 2. 扩展运算符(...)也可以用于将 Set 展开成一个数组字面量。
上一篇:js setdate()
下一篇:js set方法
Laravel PHP 深圳智简公司。版权所有©2023-2043 LaravelPHP 粤ICP备2021048745号-3
Laravel 中文站