从 JS 数组中删除重复值 [英] Remove duplicate values from JS array
本文介绍了从 JS 数组中删除重复值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个非常简单的 JavaScript 数组,它可能包含也可能不包含重复项.
I have a very simple JavaScript array that may or may not contain duplicates.
var names = ["Mike","Matt","Nancy","Adam","Jenny","Nancy","Carl"];
我需要删除重复项并将唯一值放入新数组中.
I need to remove the duplicates and put the unique values in a new array.
我可以指出我尝试过的所有代码,但我认为它没有用,因为它们不起作用.我也接受 jQuery 解决方案.
I could point to all the codes that I've tried but I think it's useless because they don't work. I accept jQuery solutions too.
推荐答案
使用 jQuery 又快又脏:
Quick and dirty using 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);
});
这篇关于从 JS 数组中删除重复值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文