我有一个列表如下:
list = ["a", "a", "b", "c", "d", "e", "a,b,f"]
我使用 new_Set(list) 并得到以下结果:
["a", "b", "c", "d", "e", "a,b,f"]
我想得到独特的对象,如何用','分割它们?
我需要的是:
["a", "b", "c", "d", "e", "f"]
我有一个列表如下:
list = ["a", "a", "b", "c", "d", "e", "a,b,f"]
我使用 new_Set(list) 并得到以下结果:
["a", "b", "c", "d", "e", "a,b,f"]
我想得到独特的对象,如何用','分割它们?
我需要的是:
["a", "b", "c", "d", "e", "f"]
TA贡献1631条经验 获得超3个赞
您可以join
将整个数组创建一个逗号分隔的字符串并将其拆分为,
:
new Set(String(list).split(","))
或者
new Set(list.join(",").split(","))
这是一个片段:
const list = ["a", "a", "b", "c", "d", "e", "a,b,f"],
unique = new Set(list.join(",").split(","))
console.log(
Array.from(unique)
)
TA贡献1548条经验 获得超5个赞
您可以平面映射拆分值。
const
list = ["a", "a", "b", "c", "d", "e", "a,b,f"],
unique = new Set(list.flatMap(s => s.split(',')));
console.log(...unique);
举报