我有一组数字,我需要确保它们是唯一的。我在互联网上找到了下面的代码片段,它工作得很好,直到数组中有一个零。我在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数组中删除重复值
类似的问题:
获取数组中的所有非唯一值(即:重复/多次出现)
下面是另一种使用比较器的方法(我更关心干净的代码而不是性能):
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);
}
[...new Set(duplicates)]
这是从MDN Web文档中引用的最简单的一个。
const numbers = [2,3,4,4,2,3,3,4,4,5,5,6,6,7,5,32,3,4,5]
console.log([...new Set(numbers)]) // [2, 3, 4, 5, 6, 7, 32]
要删除重复项,可能有两种情况。首先,所有数据都不是对象,其次所有数据都是对象。
如果所有数据都是任何类型的原始数据类型,如int、float、string等,那么您可以遵循以下一种
const uniqueArray = [...new Set(oldArray)]
但假设您的数组包含以下JS对象
{
id: 1,
name: 'rony',
email: 'rony@example.com'
}
然后,要获取所有唯一的对象,可以执行以下操作
let uniqueIds = [];
const uniqueUsers = oldArray.filter(item => {
if(uniqueIds.includes(item.id)){
return false;
}else{
uniqueIds.push(item.id);
return true;
}
})
您也可以使用此方法使任何类型的数组成为唯一的。只需将跟踪键保留在uniqueIds数组上。