jQuery - 从字符串数组中删除重复项 [英] jQuery - remove duplicates from an array of strings
问题描述
可能的重复:
在 JavaScript 数组中查找重复值的最简单方法>
jQuery.unique 在字符串数组上
我正在尝试通过广度优先搜索来获取邻居列表(具体来说:阻止)我的 function getWholeList(ballid)
返回一个数组,如
I'm trying to get a neighbor list by breadth-first search (to be specific: the indexes of same color neighbor balls in Block'd)
my function getWholeList(ballid)
return an array like
thelist=["ball_1","ball_13","ball_23","ball_1"]
当然也有重复的.
我试图用 jQuery.unique() 删除它们;但我猜它不适用于字符串,那么有没有办法做到这一点(使数组独一无二)?
I tried to remove them with jQuery.unique(); but it does not work with strings I guess, so is there any way for this(making the array unique) ?
感谢您的帮助..
推荐答案
The jQuery unique
方法 仅适用于 DOM 元素数组.
The jQuery unique
method only works on an array of DOM elements.
You can easily make your own uniqe function using the each
and inArray
methods:
function unique(list) {
var result = [];
$.each(list, function(i, e) {
if ($.inArray(e, result) == -1) result.push(e);
});
return result;
}
演示:http://jsfiddle.net/Guffa/Askwb/
这篇关于jQuery - 从字符串数组中删除重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!