我试图找出一种有效的方法来删除数组中重复的对象,并寻找最有效的答案。我环顾互联网,一切似乎都在使用原始数据......或者对于大型数组来说不可扩展。这是我当前的实现,可以改进并且希望尝试避免标签。
Test.prototype.unique = function (arr, artist, title, cb) {
console.log(arr.length);
var n, y, x, i, r;
r = [];
o: for (i = 0, n = arr.length; i < n; i++) {
for (x = 0, y = r.length; x < y; x++) {
if (r[x].artist == arr[i].artist && r[x].title == arr[i].title) {
continue o;
}
}
r.push(arr[i]);
}
cb(r);
};
该数组看起来像这样:
[{title: sky, artist: jon}, {title: rain, artist: Paul}, ....]
顺序并不重要,但如果排序可以提高效率,那么我就准备好迎接挑战......
对于那些不知道 o 是一个标签的人来说,它只是说跳回循环而不是推送到新数组。
纯 javascript 请不要使用库。
到目前为止的答案:
以下答案的性能测试:http://jsperf.com/remove-duplicates-for-loops http://jsperf.com/remove-duplicates-for-loops