jQuery - 从字符串数组中删除重复项 [英] jQuery - remove duplicates from an array of strings
本文介绍了jQuery - 从字符串数组中删除重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正试图通过广度优先搜索获得邻居列表(具体来说: Block'd )
我的函数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"]
当然还有重复项。
I试图用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) ?
感谢您的帮助..
推荐答案
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屋!
查看全文