如何使用Array.map函数从数组数组中获取新集合?
现在正在探索 Sets,但无法从其中的 x 数组数组中获取新 Sets。 我在 new Set() 中使用 Array.map 方法。
这是我正在尝试做的事情,它看起来很合乎逻辑,但最终只有来自第一个数组的值:
new Set(...array.map(x => x.value));
了解我做错了什么以及它应该如何
更新: 为了更清楚地说明我的需求:
const array = [[1,1,3,4], [2,2,3,4], [1,1,3,5]];
new Set(...array.map(x => x));
我的目标是拥有 [1,2,3,4,5]
但获得 [1,3,4]
解决方案:
new Set(array.map(x => x.value).flat())
Exploring Sets now and could not get a new Set from an array of x arrays in it.
I what to use Array.map method inside new Set().
Here is what I'm trying to do, and it seems logical, but in the end having values only from the 1st array:
new Set(...array.map(x => x.value));
Will be awesome to understand what I am doing wrong and how it should look like
Update:
To be more clear in my need:
const array = [[1,1,3,4], [2,2,3,4], [1,1,3,5]];
new Set(...array.map(x => x));
My goal to have [1,2,3,4,5]
but getting [1,3,4]
Solution:
new Set(array.map(x => x.value).flat())
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只需要数组,而不是传播参数:
from <代码> set :
,您首先需要一个公寓:
You need only the array, not spreaded parameters:
From
Set
:With a nested array, you need a flat it first: