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

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

问题描述

可能的重复:
在 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.

您可以使用 eachinArray 方法:

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天全站免登陆