javascript – 从JS数组中删除重复值

参见英文答案 > Get all unique values in a JavaScript array (remove duplicates)                                    73个
我有一个非常简单的JavaScript数组,可能包含也可能不包含重复项.

var names = ["Mike","Matt","Nancy","Adam","Jenny","Nancy","Carl"];

我需要删除重复项并将唯一值放在一个新数组中.

我可以指出我尝试的所有代码,但我认为它没用,因为它们不起作用.我也接受jQuery解决方案.

类似的问题:

> Get all non-unique values (i.e.: duplicate/more than one occurrence) in an array

解决方法:

使用jQuery快速而肮脏:

var names = ["Mike","Matt","Nancy","Adam","Jenny","Nancy","Carl"];
var uniqueNames = [];
$.each(names, function(i, el){
    if($.inArray(el, uniqueNames) === -1) uniqueNames.push(el);
});
上一篇:在JavaScript中复制数组的最快方法 – 切片与’for’循环


下一篇:检查重复的Javascript对象