JS:删除数组中的重复值,包括原始值 [英] JS: remove duplicate values in array, including the original

查看:121
本文介绍了JS:删除数组中的重复值,包括原始值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁知道如何删除包含原始值的数组中的重复项?我遇到了不同的片段,如
这个和其他一些但没有特别删除原件节点同时。小心分享一些小片段? TIA!

Does anyone know how to remove duplicates in an array including the original value? I came across different snippets like this and this and some others but none in particular is removing the original node at the same time. Care to share a little snippet ? TIA!

示例:

[1, 1, 2, 3, 5, 5] -> [2, 3]


推荐答案

您可以尝试过滤您的数组基于该数组中特定值的出现次数:

You could try filtering your array based on the number of occurrences of a specific value in that array:

var arr = [1, 1, 2, 3, 5, 5]

var res = arr.filter((el, _, arr) => {
      return arr.filter(el2 => el2 === el).length === 1
})

console.log(res)

这篇关于JS:删除数组中的重复值,包括原始值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆