JS:删除数组中的重复值,包括原始值 [英] JS: remove duplicate values in array, including the original
本文介绍了JS:删除数组中的重复值,包括原始值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有谁知道如何删除数组中的重复项,包括原始值?我遇到了不同的片段,例如 this和 this 和其他一些但没有特别是同时删除原始节点.愿意分享一个小片段吗?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屋!
查看全文