无法从数组创建新集合

huangapple go评论58阅读模式
英文:

Unable to Create New Set From Array

问题

我正在尝试从一个数组创建新的集合,但它返回一个空对象。我做错了什么?

const myArray = ["value1", "value2", "value3", "value1", "value2"];
const mySet = new Set(myArray);
Logger.log(mySet);


<details>
<summary>英文:</summary>

I am trying to create new Sets from an array but it is returning an empty object. What am I doing wrong?

const myArray = ["value1", "value2", "value3", "value1", "value2"];
const mySet = new Set(myArray);
Logger.log(mySet);



</details>


# 答案1
**得分**: 0

`new Set()` 返回 Set 对象。当您想查看该值时,可以尝试以下修改:

### 从:
	Logger.log(mySet);

### 到:
	Logger.log([...mySet]);

或

	Logger.log(Array.from(mySet));

### 参考:
- [Spread 语法 (...)](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Operators/Spread_syntax)
- [Array.from()](https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/from)

<details>
<summary>英文:</summary>

`new Set()` returns the Set object. When you want to see the value, how about the following modification?

### From:
	Logger.log(mySet);

### To:

	Logger.log([...mySet]);

or

	Logger.log(Array.from(mySet));

### References:
- [Spread syntax (...)](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax)
- [Array.from()](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from)


</details>



huangapple
  • 本文由 发表于 2023年2月24日 08:18:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/75551571.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定