我有一组数字,我需要确保它们是唯一的。我在互联网上找到了下面的代码片段,它工作得很好,直到数组中有一个零。我在Stack Overflow上找到了另一个脚本,看起来几乎与它完全一样,但它不会失败。

所以为了帮助我学习,有人能帮我确定原型脚本哪里出错吗?

Array.prototype.getUnique = function() {
 var o = {}, a = [], i, e;
 for (i = 0; e = this[i]; i++) {o[e] = 1};
 for (e in o) {a.push (e)};
 return a;
}

重复问题的更多答案:

从JS数组中删除重复值

类似的问题:

获取数组中的所有非唯一值(即:重复/多次出现)


当前回答

一种可扩展、快速、高效且易于阅读的现代方法,使用iter ops库:

import {pipe, distinct} from 'iter-ops';

const input = [1, 1, 2, 2, 2, 3]; // our data

const i = pipe(input, distinct()); // distinct iterable

console.log([...i]); //=> [1, 2, 3]

如果您的输入是一个对象数组,则只需为不同的运算符提供一个键选择器。

其他回答

Array.prototype.getUnique = function() {
    var o = {}, a = []
    for (var i = 0; i < this.length; i++) o[this[i]] = 1
    for (var e in o) a.push(e)
    return a
}

你可以试试这个:

function removeDuplicates(arr){
  var temp = arr.sort();
  for(i = 0; i < temp.length; i++){
    if(temp[i] == temp[i + 1]){
      temp.splice(i,1);
      i--;
    }
  }
  return temp;
}

One Liner,纯JavaScript

使用ES6语法

list=list.filter((x,i,a)=>a.indexOf(x)==i)

x --> item in array
i --> index of item
a --> array reference, (in this case "list")

使用ES5语法

list = list.filter(function (x, i, a) { 
    return a.indexOf(x) == i; 
});

浏览器兼容性:IE9+

使用One Liner在对象阵列中查找唯一

const uniqueBy = (x,f)=>Object.values(x.reduce((a,b)=>((a[f(b)]=b),a),{}));
// f -> should must return string because it will be use as key

const data = [
  { comment: "abc", forItem: 1, inModule: 1 },
  { comment: "abc", forItem: 1, inModule: 1 },
  { comment: "xyz", forItem: 1, inModule: 2 },
  { comment: "xyz", forItem: 1, inModule: 2 },
];

uniqueBy(data, (x) => x.forItem +'-'+ x.inModule); // find unique by item with module
// output
// [
//   { comment: "abc", forItem: 1, inModule: 1 },
//   { comment: "xyz", forItem: 1, inModule: 2 },
// ];

// can also use for strings and number or other primitive values

uniqueBy([1, 2, 2, 1], (v) => v); // [1, 2]
uniqueBy(["a", "b", "a"], (v) => v); // ['a', 'b']

uniqueBy(
  [
    { id: 1, name: "abc" },
    { id: 2, name: "xyz" },
    { id: 1, name: "abc" },
  ],
  (v) => v.id
);
// output
// [
//   { id: 1, name: "abc" },
//   { id: 2, name: "xyz" },
// ];

使用字段[2]作为Id,创建唯一数组数组:

常量arr=[['497','Q0','WTX091-B06-138','0','1.000000','GROUP001'],['497','Q0','WTX091-B09-92','1','0.866899288','GROUP001'],['497','Q0','WTX091-B09-92','2','0.846036819','GROUP001'],['497','Q0','WTX091-B09-57','3','0.835025326','GROUP001'],['497','Q0','WTX091-B43-79','4','0.765068215','GROUP001'],['497','Q0','WTX091-B43-56','5','0.764211464','GROUP001'],['497','Q0','WTX091-B44-448','6','0.761701704','GROUP001'],['497','Q0','WTX091-B44-12','7','0.761701704','GROUP001'],['497','Q0','WTX091-B49-128','8','0.747434800','GROUP001'],['497','Q0','WTX091-B18-17','9','0.746724770','GROUP001'],['497','Q0','WTX091-B19-374','10','0.733379549','GROUP001'],['497','Q0','WTX091-B19-344','11','0.731421782','GROUP001'],['497','Q0','WTX091-B09-92','12','0.726450470','GROUP001'],['497','Q0','WTX091-B19-174','13','0.712757036','GROUP001']];arr.filter((val1,idx1,arr)=>~val1.indexOf(val1[2])&&!(arr.filter((val2,idx2)=>~val2.indexOf(val1[2])&&idx2<idx1).长度));控制台日志(arr);