如何在数组中获得唯一值的列表?我总是必须使用第二个数组,或者在JavaScript中有类似于java的hashmap的东西吗?

我将只使用JavaScript和jQuery。不能使用其他库。


当前回答

既然我在@Rocket的回答的评论中谈到了它,我不妨提供一个不使用库的示例。这需要两个新的原型功能,包含和唯一

Array.prototype.contains =函数(v) { For (var I = 0;I < this.length;我+ +){ If (this[i] === v)返回true; } 返回错误; }; Array.prototype.unique = function() { Var arr = []; For (var I = 0;I < this.length;我+ +){ If (!arr.contains(this[i])) { arr.push(这[我]); } } 返回arr; } Var duplicate = [1,3,4,2,1,2,3,8]; Var uniques = duplicate .unique();// result = [1,3,4,2,8] console.log(独立);

为了获得更高的可靠性,您可以用MDN的indexOf shim替换contains,并检查每个元素的indexOf是否等于-1:documentation

其他回答

这里有一个更清晰的ES6解决方案,我看到这里没有包括它。它使用Set和展开操作符:…

var a = [1, 1, 2];

[... new Set(a)]

返回[1,2]

如果你想保持原始数组不变,

您需要第二个数组来包含第一个-的唯一元素

大多数浏览器都有Array.prototype.filter:

const unique = array1.filter((item, index, array) => array.indexOf(item) === index);


//if you need a 'shim':
Array.prototype.filter= Array.prototype.filter || function(fun, scope){
    var T= this, A= [], i= 0, itm, L= T.length;
    if(typeof fun== 'function'){
        while(i<L){
            if(i in T){
                itm= T[i];
                if(fun.call(scope, itm, i, T)) A[A.length]= itm;
            }
            ++i;
        }
    }
    return A;
}
 Array.prototype.indexOf= Array.prototype.indexOf || function(what, i){
        if(!i || typeof i!= 'number') i= 0;
        var L= this.length;
        while(i<L){
            if(this[i]=== what) return i;
            ++i;
        }
        return -1;
    }

使用EcmaScript 2016,你可以简单地像这样做。

 var arr = ["a", "a", "b"];
 var uniqueArray = Array.from(new Set(arr)); // Unique Array ['a', 'b'];

集合总是唯一的,使用array. from()可以将集合转换为数组。参考一下文件。

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set

你可以输入带有重复元素的数组,下面的方法将返回带有唯一元素的数组。

function getUniqueArray(array){
    var uniqueArray = [];
    if (array.length > 0) {
       uniqueArray[0] = array[0];
    }
    for(var i = 0; i < array.length; i++){
        var isExist = false;
        for(var j = 0; j < uniqueArray.length; j++){
            if(array[i] == uniqueArray[j]){
                isExist = true;
                break;
            }
            else{
                isExist = false;
            }
        }
        if(isExist == false){
            uniqueArray[uniqueArray.length] = array[i];
        }
    }
    return uniqueArray;
}

快速,紧凑,无嵌套循环,适用于任何对象,不只是字符串和数字,接受谓词,只有5行代码!!

function findUnique(arr, predicate) {
  var found = {};
  arr.forEach(d => {
    found[predicate(d)] = d;
  });
  return Object.keys(found).map(key => found[key]); 
}

示例:按类型查找唯一项:

var things = [
  { name: 'charm', type: 'quark'},
  { name: 'strange', type: 'quark'},
  { name: 'proton', type: 'boson'},
];

var result = findUnique(things, d => d.type);
//  [
//    { name: 'charm', type: 'quark'},
//    { name: 'proton', type: 'boson'}
//  ] 

如果你想让它找到第一个唯一的项目,而不是最后一个,在那里添加一个find . hasownproperty()检查。