我有一组数字,我需要确保它们是唯一的。我在互联网上找到了下面的代码片段,它工作得很好,直到数组中有一个零。我在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数组中删除重复值

类似的问题:

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


当前回答

在ES6/以后

仅获取唯一值

  let a = [
           { id: 1, name: "usman" },
           { id: 2, name: "zia" },
           { id: 3, name: "usman" },
          ];
const unique = [...new Set(a.map((item) => item.name))];
console.log(unique); // ["usman", "zia"]

获取唯一对象

const myObjArray = [
                       { id: 1, name: "usman" },
                       { id: 2, name: "zia" },
                       { id: 3, name: "usman" },
                   ];
// Creates an array of objects with unique "name" property values.
let uniqueObjArray = [
  ...new Map(myObjArray.map((item) => [item["name"], item])).values(),
];

console.log("uniqueObjArray", uniqueObjArray);

其他回答

下面是另一种使用比较器的方法(我更关心干净的代码而不是性能):

const list = [
    {name: "Meier"},
    {name: "Hans"},
    {name: "Meier"},
]
const compare = (a, b) => a.name.localeCompare(b.name);
const uniqueNames = list.makeUnique(compare);
uniqueNames.pushIfAbsent({name: "Hans"}, compare);

原型声明:

declare global {
    interface Array<T>  {
        pushIfAbsent(item: T, compare:(a:T, b:T)=>number): number;
    }
    interface Array<T>  {
        makeUnique(compare:(a:T, b:T)=>number): Array<T>;
    }
}
Array.prototype.pushIfAbsent = function <T>(this:T[], item:T, compare:(a:T, b:T)=>number) {
    if (!this.find(existing => compare(existing, item)===0)) {
        return this.push(item)
    } else {
        return this.length;
    }
}
Array.prototype.makeUnique = function <T>(this:T[], compare:(a:T, b:T)=>number) {
    return this.filter((existing, index, self) => self.findIndex(item => compare(existing, item) == 0) == index);
}

使用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" },
// ];

重复数据消除通常需要给定类型的相等运算符。然而,使用eq函数会阻止我们以有效的方式使用Set来确定重复项,因为Set返回到==。如您所知,==不适用于引用类型。所以,如果被卡住了,我们会很好,对吧?

解决方法是简单地使用一个转换器函数,它允许我们将一个(引用)类型转换为我们可以使用Set实际查找的类型。例如,如果数据结构不包含任何函数,我们可以使用哈希函数或JSON.stringify数据结构。

通常我们只需要访问一个属性,然后我们就可以比较它而不是Object的引用。

以下是满足这些要求的两个组合子:

常量重复数据消除On=k=>xs=>{const s=new Set();返回xs.filter(o=>s有(o[k])? 无效的:(s.add(o[k]),o[k]]);};常量重复数据消除By=f=>xs=>{const s=new Set();返回xs.filter(x=>{常量r=f(x);返回s.has(r)? 无效的:(s.add(r),x);});};const xs=[{foo:“a”},{foo:“b”};控制台日志(重复数据删除打开(“foo”)(xs));//〔{foo:“a”},{foo:“b”}、{foo:“a”}和{foo:“c”}〕控制台日志(重复数据删除方式(o=>o.foo.toLowerCase())(xs));//〔{foo:“a”}、{foo:“b”},{foo:“c”}〕

使用这些组合器,我们可以非常灵活地处理各种重复数据消除问题。这不是禁食的方法,而是最具表现力和通用性的方法。

对我来说,这是最简单的解决方案

//检查数组是否相等的方法常量a=['a','B','C'].sort().toString()常量b=['A','C','b'].sort().toString()console.log(a===b);//真的//测试用例常量数据=[{组:“A”,名称:“SD”},{组:“B”,名称:“FI”},{组:“A”,名称:“SD”},{组:“B”,名称:“CO”}];//返回一个不带重复定位的新数组函数唯一(数据){return data.reduce(函数(累加器,currentValue){//转换为字符串以检查它们是否为相同的值。const currentKeys=对象.keys(currentValue).sort().toString();const currentValues=对象.values(currentValue).sort().toString();let hasObject=falsefor(累加器的常量对象){//将键和值转换为字符串,以便我们可以//看看它们是否与当前值相等constkeys=对象.keys(obj).sort().toString();const values=Object.values(obj).sort().toString();//检查键和值是否相等if(keys==currentKeys&&values==currentValues){hasObject=真}}//如果对象不存在,则推送该对象。if(!hasObject){累加器推力(currentValue)}回流蓄能器}, []);}//运行测试用例console.log(唯一(数据));//[{组:“A”,名称:“SD”},{组为“B”,名称为“FI”}、{组“B”、名称为“CO”}]

如果顺序不重要,那么我们可以创建一个哈希,并获取密钥以创建唯一的数组。

var ar = [1,3,4,5,5,6,5,6,2,1];
var uarEle = {};
links.forEach(function(a){ uarEle[a] = 1; });
var uar = keys(uarEle)

uar将具有唯一的数组元素。