在JQuery中创建一个包含Unique元素的数组

时间:2014-06-12 06:25:27

标签: javascript jquery unique

我得到一个类似

的数组
["cat, Kitchen", "dog", "Kitchen, dog", "Kitchen"].

如何从中获取一个独特的数组。我尝试了split(', '),但我在浏览器中收到错误.split is not a function

我希望最终的数组是:

["cat", "Kitchen", "dog"]

请让我知道如何为此输出编写jQuery

提前致谢....

6 个答案:

答案 0 :(得分:1)

jQuery.unique([" cat"," Kitchen"," dog"," Kitchen"," dog&# 34;,"厨房"]。加入(",")。拆分(","))

答案 1 :(得分:1)

function unique(array) {
    if (array) {
        var found = {};
        array = array.join(",").split(",").filter(function (x) {
            x = x.trim();
            return (found[x] ? false : (found[x] = x));
        })
    }
    return array;
}

console.log(unique(["cat, Kitchen", "dog", "Kitchen, dog", "Kitchen"]));

演示:http://jsfiddle.net/diode/2P2Kg/

仅在Chrome中进行测试,因此不确定这是否适用于所有浏览器。可能不是完美的解决方案,但这是您可以继续前进的方向。

答案 2 :(得分:1)

试试 Demo Fiddle

var arr = ["cat", "Kitchen", "dog", "Kitchen", "dog", "Kitchen"];
unique(arr);


function unique(arrList) {
    var final = [];
    $.each(arrList, function (i, e) {
        if ($.inArray(e, final) == -1) final.push(e);
    });
    console.log(final);
}

注意:

您声明数组的方式是错误的 - ["cat, Kitchen", "dog", "Kitchen, dog", "Kitchen"]。它没有重复的元素 - "cat, Kitchen"被视为一个元素。

应为["cat", "Kitchen", "dog", "Kitchen", "dog", "Kitchen"].

答案 3 :(得分:1)

1)错过了关闭数组列表中的双引号。

2)使用以下函数在javascript中获取唯一数组

var uniqueArray =["cat", "Kitchen", "dog", "Kitchen", "dog", "Kitchen"];

function GetUnique(inputArray)
{
    var outputArray = [];

    for (var i = 0; i < inputArray.length; i++)
    {
        if ((jQuery.inArray(inputArray[i], outputArray)) == -1)
        {
            outputArray.push(inputArray[i]);
        }
    }

    return outputArray;
};

alert(GetUnique(uniqueArray))

DEMO

或与jquery一起使用

var uniqueArray = ["cat", "Kitchen", "dog", "Kitchen", "dog", "Kitchen"];

alert($.unique(uniqueArray));

DEMO

答案 4 :(得分:0)

编辑:扩展已接受的答案(重复数据删除条目):

["cat, Kitchen", "dog", "Kitchen, dog", "Kitchen"].join(",").split(",").
  map(function(entry){return entry.trim();}).
  filter(function(entry, i, a) { return a.indexOf(entry) === i; });

我通过一系列转换解决了它,不需要jQuery。 但是,无法更好地命名,因为我不知道什么是真正的交易。

function doItsThing (crazyArray) {

  function toArrays (entry) {
    return entry.split(',');
  }
  function toAFlatArray (arrays, entry) {
    return arrays.concat(entry);
  }
  function trim (entry) {
    return entry.trim();
  }
  function dedupe (entry, index, array) {
    return array.indexOf(entry) == index;
  }

  return crazyArray.
    map(toArrays).
    reduce(toAFlatArray, []).
    map(trim).
    filter(dedupe);
}

doItsThing(["cat, Kitchen", "dog", "Kitchen, dog", "Kitchen"])

或者,如果你想要全部内联(尽管不是那么可读,与上面相比,imo):

function doItsThing (crazyArray) {
  return crazyArray.
    map(function (entry) {
      return entry.split(',');
    }).
    reduce(function (collection, entry) {
      return collection.concat(entry);
    }, []).
    map(function (entry) {
      return entry.trim();
    }).
    filter(function (entry, index, collection) {
      return collection.indexOf(entry) == index;
    });
}

doItsThing(["cat, Kitchen", "dog", "Kitchen, dog", "Kitchen"])

当然可以改进,但这是个主意。

答案 5 :(得分:0)

var streetaddress_hs = ["cat", "Kitchen", "dog", "Kitchen", "dog", "Kitchen"];
var streetaddress2 = [];
$.each(streetaddress_hs, function (i, el) {
    if ($.inArray(el, streetaddress2) === -1) streetaddress2.push(el);
});
availableTags = streetaddress2;
alert(availableTags);