如何在数组中获得唯一值的列表?我总是必须使用第二个数组,或者在JavaScript中有类似于java的hashmap的东西吗?
我将只使用JavaScript和jQuery。不能使用其他库。
如何在数组中获得唯一值的列表?我总是必须使用第二个数组,或者在JavaScript中有类似于java的hashmap的东西吗?
我将只使用JavaScript和jQuery。不能使用其他库。
当前回答
或者对于那些寻找与当前浏览器兼容的一行程序(简单而实用)的人:
Let a = ["1", "1", "2", "3", "3", "1"]; let unique = a.filter((item, i, ar) => ar. indexof (item) === i); console.log(独特的);
更新2021 我建议你去看看Charles Clayton的答案,在JS的最新修改中,甚至有更简洁的方法来做到这一点。
更新18-04-2017
它看起来就像'Array.prototype。Includes '现在在主流浏览器的最新版本中得到了广泛支持(兼容性)
更新29-07-2015:
目前正在计划让浏览器支持标准化的“Array.prototype”。方法,虽然没有直接回答这个问题;往往是相关的。
用法:
["1", "1", "2", "3", "3", "1"].includes("2"); // true
polyfill(浏览器支持,来自mozilla):
// https://tc39.github.io/ecma262/#sec-array.prototype.includes
if (!Array.prototype.includes) {
Object.defineProperty(Array.prototype, 'includes', {
value: function(searchElement, fromIndex) {
// 1. Let O be ? ToObject(this value).
if (this == null) {
throw new TypeError('"this" is null or not defined');
}
var o = Object(this);
// 2. Let len be ? ToLength(? Get(O, "length")).
var len = o.length >>> 0;
// 3. If len is 0, return false.
if (len === 0) {
return false;
}
// 4. Let n be ? ToInteger(fromIndex).
// (If fromIndex is undefined, this step produces the value 0.)
var n = fromIndex | 0;
// 5. If n ≥ 0, then
// a. Let k be n.
// 6. Else n < 0,
// a. Let k be len + n.
// b. If k < 0, let k be 0.
var k = Math.max(n >= 0 ? n : len - Math.abs(n), 0);
// 7. Repeat, while k < len
while (k < len) {
// a. Let elementK be the result of ? Get(O, ! ToString(k)).
// b. If SameValueZero(searchElement, elementK) is true, return true.
// c. Increase k by 1.
// NOTE: === provides the correct "SameValueZero" comparison needed here.
if (o[k] === searchElement) {
return true;
}
k++;
}
// 8. Return false
return false;
}
});
}
其他回答
使用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
在Javascript中不是原生的,但是很多库都有这个方法。
Underscore.js的_.uniq(array) (link)工作得很好(来源)。
我在纯JS中尝试过这个问题。 我遵循了以下步骤1。对给定数组进行排序,2。遍历排序数组,3。用当前值验证前一个值和下一个值
// JS
var inpArr = [1, 5, 5, 4, 3, 3, 2, 2, 2,2, 100, 100, -1];
//sort the given array
inpArr.sort(function(a, b){
return a-b;
});
var finalArr = [];
//loop through the inpArr
for(var i=0; i<inpArr.length; i++){
//check previous and next value
if(inpArr[i-1]!=inpArr[i] && inpArr[i] != inpArr[i+1]){
finalArr.push(inpArr[i]);
}
}
console.log(finalArr);
Demo
使用jQuery,这是一个数组唯一的函数我做:
Array.prototype.unique = function () {
var arr = this;
return $.grep(arr, function (v, i) {
return $.inArray(v, arr) === i;
});
}
console.log([1,2,3,1,2,3].unique()); // [1,2,3]
如果您不需要太担心旧的浏览器,这正是set的设计目的。
Set对象允许您存储任何类型的惟一值 原语值或对象引用。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set
const set1 = new Set([1, 2, 3, 4, 5, 1]);
// returns Set(5) {1, 2, 3, 4, 5}