JavaScript:在2D数组中删除重复
我在JavaScript中有一个像这样的2D数组:
result=[["user1", "java"], ["user2", "python"],["user1", "java"], ["user1", "C++"], ["user1", "java"], ["user2", "Python"]....]
您可以看到 [“ user1”,“ java”]
来了3次, [“ user2”,“ python”]
2次。
我想清洁此数组,以便每个几个元素只能出现一次。这意味着如果“ user1”可以再次出现,则应使用另一种语言,而不是“ java”。 有人可以帮我吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用
降低
,join
和
You can do this with a single loop over the array using
reduce
,join
andSet
as:您可以尝试这个:
You could try this one:
您可以创建一个通过加入用户和语言字符串,从数组中删除重复项。然后只需将字符串拆分回到所需的数组即可。
相关文档:
You can create a
Set
out of the array by joining the user and language strings, which would remove the duplicates. And then simply split the strings back to get the desired array.Relevant Documentations: