jQuery - 从字符串数组中删除重复项 [英] jQuery - remove duplicates from an array of strings

查看:181
本文介绍了jQuery - 从字符串数组中删除重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

在JavaScript数组中查找重复值的最简单方法

对字符串数组的jQuery.unique

我正试图通过广度优先搜索获得邻居列表(具体来说: 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.

您可以使用 每个 inArray 方法:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆