英文:
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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论