从一个字符串数组中删除副本[复制]

时间:2021-11-27 07:40:53

Possible Duplicate:
Easiest way to find duplicate values in a JavaScript array
jQuery.unique on an array of strings

可能的重复:在JavaScript数组jQuery中找到重复值的最简单方法。在字符串数组中唯一的。

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

我试图通过宽度优先搜索(具体是:块中相同颜色的邻居球的索引)来获得一个邻居列表,我的函数getWholeList(ballid)返回一个数组。

thelist=["ball_1","ball_13","ball_23","ball_1"]

and of course there are duplicates.

当然也有重复的。

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()删除它们;但是我猜它不能处理字符串,所以有什么方法(使数组唯一)吗?

Thanks for any help..

感谢任何帮助。

5 个解决方案

#1


58  

The jQuery unique method only works on an array of DOM elements.

jQuery惟一方法只对DOM元素数组起作用。

You can easily make your own uniqe function using the each and inArray methods:

您可以使用每个和inArray方法轻松地创建您自己的uniqe函数:

function unique(list) {
  var result = [];
  $.each(list, function(i, e) {
    if ($.inArray(e, result) == -1) result.push(e);
  });
  return result;
}

Demo: http://jsfiddle.net/Guffa/Askwb/

演示:http://jsfiddle.net/Guffa/Askwb/

#2


10  

As a non jquery solution you could use the Arrays filter method like this:

作为一种非jquery解决方案,您可以使用如下的数组筛选方法:

var thelist=["ball_1","ball_13","ball_23","ball_1"], 
    thelistunique = thelist.filter(
                 function(a){if (!this[a]) {this[a] = 1; return a;}},
                 {}
                );
//=> thelistunique = ["ball_1", "ball_13", "ball_23"]

As extension to Array.prototype (using a shortened filter callback)

作为扩展数组。原型(使用缩短的过滤回调)

Array.prototype.uniq = function(){
  return this.filter(
      function(a){return !this[a] ? this[a] = true : false;}, {}
  );
}
thelistUnique = thelist.uniq(); //=> ["ball_1", "ball_13", "ball_23"]

[Edit 2017] An ES6 take on this could be:

[编辑2017]ES6对此的看法可能是:

Array.from(["ball_1","ball_13","ball_23","ball_1"]
           .reduce( (a, b) => a.set(b, b) , new Map()))
     .map( v => v[1] );

#3


8  

Try this one - Array.unique()

试试这个- Array.unique()

Array.prototype.unique =
  function() {
    var a = [];
    var l = this.length;
    for(var i=0; i<l; i++) {
      for(var j=i+1; j<l; j++) {
        // If this[i] is found later in the array
        if (this[i] === this[j])
          j = ++i;
      }
      a.push(this[i]);
    }
    return a;
  };
thelist=["ball_1","ball_13","ball_23","ball_1"]
thelist=thelist.unique()

#4


2  

jQuery.unique() only works for array of DOM elements. Take a look at this (enhanced version of unique) :

unique()只适用于DOM元素的数组。看看这个(unique的增强版):

Enhanced unique

增强的独特

#5


2  

There is a JavaScript port of the PHP array_unique function here: http://phpjs.org/functions/array_unique

这里有一个PHP array_unique函数的JavaScript端口:http://phpjs.org/functions/array_unique

function array_unique (inputArr) {
    var key = '',
        tmp_arr2 = {},
        val = '';

    var __array_search = function (needle, haystack) {
        var fkey = '';
        for (fkey in haystack) {
            if (haystack.hasOwnProperty(fkey)) {
                if ((haystack[fkey] + '') === (needle + '')) {
                    return fkey;
                }
            }
        }
        return false;
    };

    for (key in inputArr) {
        if (inputArr.hasOwnProperty(key)) {
            val = inputArr[key];
            if (false === __array_search(val, tmp_arr2)) {
                tmp_arr2[key] = val;
            }
        }
    }

    return tmp_arr2;
}

Or as of later JS:

或后来的JS:

arr.filter((v, p) => arr.indexOf(v) == p)

#1


58  

The jQuery unique method only works on an array of DOM elements.

jQuery惟一方法只对DOM元素数组起作用。

You can easily make your own uniqe function using the each and inArray methods:

您可以使用每个和inArray方法轻松地创建您自己的uniqe函数:

function unique(list) {
  var result = [];
  $.each(list, function(i, e) {
    if ($.inArray(e, result) == -1) result.push(e);
  });
  return result;
}

Demo: http://jsfiddle.net/Guffa/Askwb/

演示:http://jsfiddle.net/Guffa/Askwb/

#2


10  

As a non jquery solution you could use the Arrays filter method like this:

作为一种非jquery解决方案,您可以使用如下的数组筛选方法:

var thelist=["ball_1","ball_13","ball_23","ball_1"], 
    thelistunique = thelist.filter(
                 function(a){if (!this[a]) {this[a] = 1; return a;}},
                 {}
                );
//=> thelistunique = ["ball_1", "ball_13", "ball_23"]

As extension to Array.prototype (using a shortened filter callback)

作为扩展数组。原型(使用缩短的过滤回调)

Array.prototype.uniq = function(){
  return this.filter(
      function(a){return !this[a] ? this[a] = true : false;}, {}
  );
}
thelistUnique = thelist.uniq(); //=> ["ball_1", "ball_13", "ball_23"]

[Edit 2017] An ES6 take on this could be:

[编辑2017]ES6对此的看法可能是:

Array.from(["ball_1","ball_13","ball_23","ball_1"]
           .reduce( (a, b) => a.set(b, b) , new Map()))
     .map( v => v[1] );

#3


8  

Try this one - Array.unique()

试试这个- Array.unique()

Array.prototype.unique =
  function() {
    var a = [];
    var l = this.length;
    for(var i=0; i<l; i++) {
      for(var j=i+1; j<l; j++) {
        // If this[i] is found later in the array
        if (this[i] === this[j])
          j = ++i;
      }
      a.push(this[i]);
    }
    return a;
  };
thelist=["ball_1","ball_13","ball_23","ball_1"]
thelist=thelist.unique()

#4


2  

jQuery.unique() only works for array of DOM elements. Take a look at this (enhanced version of unique) :

unique()只适用于DOM元素的数组。看看这个(unique的增强版):

Enhanced unique

增强的独特

#5


2  

There is a JavaScript port of the PHP array_unique function here: http://phpjs.org/functions/array_unique

这里有一个PHP array_unique函数的JavaScript端口:http://phpjs.org/functions/array_unique

function array_unique (inputArr) {
    var key = '',
        tmp_arr2 = {},
        val = '';

    var __array_search = function (needle, haystack) {
        var fkey = '';
        for (fkey in haystack) {
            if (haystack.hasOwnProperty(fkey)) {
                if ((haystack[fkey] + '') === (needle + '')) {
                    return fkey;
                }
            }
        }
        return false;
    };

    for (key in inputArr) {
        if (inputArr.hasOwnProperty(key)) {
            val = inputArr[key];
            if (false === __array_search(val, tmp_arr2)) {
                tmp_arr2[key] = val;
            }
        }
    }

    return tmp_arr2;
}

Or as of later JS:

或后来的JS:

arr.filter((v, p) => arr.indexOf(v) == p)