我需要检查一个JavaScript数组,看看是否有任何重复的值。最简单的方法是什么?我只需要找到复制的值是什么——实际上我不需要它们的索引,也不需要它们复制了多少次。
我知道我可以循环遍历数组并检查所有其他值是否匹配,但似乎应该有更简单的方法。
类似的问题:
获取JavaScript数组中所有唯一的值(删除重复值)
我需要检查一个JavaScript数组,看看是否有任何重复的值。最简单的方法是什么?我只需要找到复制的值是什么——实际上我不需要它们的索引,也不需要它们复制了多少次。
我知道我可以循环遍历数组并检查所有其他值是否匹配,但似乎应该有更简单的方法。
类似的问题:
获取JavaScript数组中所有唯一的值(删除重复值)
当前回答
大多数答案我都不喜欢。
为什么?太复杂,代码太多,效率低下,许多代码没有回答问题,即找到重复项(而不是给出一个没有重复项的数组)。
Next函数返回所有副本:
function GetDuplicates(arr) {
var i, out=[], obj={};
for (i=0; i < arr.length; i++)
obj[arr[i]] == undefined ? obj[arr[i]] ++ : out.push(arr[i]);
return out;
}
因为在大多数情况下,返回所有重复值是没有用的,而只是告诉存在哪些重复值。在这种情况下,返回一个具有唯一重复项的数组;-)
function GetDuplicates(arr) {
var i, out=[], obj={};
for (i=0; i < arr.length; i++)
obj[arr[i]] == undefined ? obj[arr[i]] ++ : out.push(arr[i]);
return GetUnique(out);
}
function GetUnique(arr) {
return $.grep(arr, function(elem, index) {
return index == $.inArray(elem, arr);
});
}
也许其他人也这么想。
其他回答
[1, 2, 2, 3, 3, 4, 5, 6, 2, 3, 50, 8, 5, 22, 1, 2, 511, 12, 50, 22].reduce(function (total, currentValue, currentIndex, arr) {
if (total.indexOf(currentValue) === -1 && arr.indexOf(currentValue) !== currentIndex)
total.push(currentValue);
return total;
}, [])
这是我的建议(ES6):
let a = [1, 2, 3, 4, 2, 2, 4, 1, 5, 6]
let b = [...new Set(a.sort().filter((o, i) => o !== undefined && a[i + 1] !== undefined && o === a[i + 1]))]
// b is now [1, 2, 4]
这是我能想到的最简单的解决办法:
const arr =[1、2、2、2 0,0,0,500,1,“,”“,”“) Const filtered = arr。filter((el, index) => arr.indexOf(el) !== index) // => filtered = [2,2,0,0, -1, 'a', 'a'] Const duplicate =[…]新的(过滤) console.log(副本) // => [2,0, -1, 'a']
就是这样。
注意:
It works with any numbers including 0, strings and negative numbers e.g. -1 - Related question: Get all unique values in a JavaScript array (remove duplicates) The original array arr is preserved (filter returns the new array instead of modifying the original) The filtered array contains all duplicates; it can also contain more than 1 same value (e.g. our filtered array here is [ 2, 2, 0, 0, -1, 'a', 'a' ]) If you want to get only values that are duplicated (you don't want to have multiple duplicates with the same value) you can use [...new Set(filtered)] (ES6 has an object Set which can store only unique values)
希望这能有所帮助。
这可能是从数组中永久删除重复项的最快方法之一 比大多数函数快10倍。safari快78倍
function toUnique(a,b,c){//array,placeholder,placeholder
b=a.length;
while(c=--b)while(c--)a[b]!==a[c]||a.splice(c,1)
}
var array=[1,2,3,4,5,6,7,8,9,0,1,2,1];
toUnique(array);
console.log(array);
测试:http://jsperf.com/wgu 演示:http://jsfiddle.net/46S7g/ 更多:https://stackoverflow.com/a/25082874/2450730
如果你不能阅读上面的代码,请阅读javascript书籍,或者这里有一些关于较短代码的解释。https://stackoverflow.com/a/21353032/2450730
编辑 正如注释中所述,此函数确实返回一个具有惟一值的数组,但是问题要求查找重复项。在这种情况下,对这个函数进行简单的修改就可以将重复项推入数组,然后使用前面的函数来移除重复项的重复项。
function theDuplicates(a,b,c,d){//array,placeholder,placeholder
b=a.length,d=[];
while(c=--b)while(c--)a[b]!==a[c]||d.push(a.splice(c,1))
}
var array=[1,2,3,4,5,6,7,8,9,0,1,2,1];
toUnique(theDuplicates(array));
我只需要找到复制的值是什么——实际上我不需要它们的索引,也不需要它们复制了多少次。
这是一个有趣而简单的任务,有许多难以阅读的答案……
打印稿
function getDuplicatedItems<T>(someArray: T[]): T[] {
// create a set to iterate through (we only need to check each value once)
const itemSet = new Set<T>(someArray);
// from that Set, we check if any of the items are duplicated in someArray
const duplicatedItems = [...itemSet].filter(
(item) => someArray.indexOf(item) !== someArray.lastIndexOf(item)
);
return duplicatedItems;
}
JavaScript
function getDuplicatedItems(someArray) {
// check for misuse if desired
// if (!Array.isArray(someArray)) {
// throw new TypeError(`getDuplicatedItems requires an Array type, received ${typeof someArray} type.`);
// }
const itemSet = new Set(someArray);
const duplicatedItems = [...itemSet].filter(
(item) => someArray.indexOf(item) !== someArray.lastIndexOf(item)
);
return duplicatedItems;
}