在对数组进行排序时删除重复项
Remove duplicates while sorting an array
我有一个数组,其中包含我需要排序的对象,并根据每个数组项的 3 个特定值删除重复项。目前我正在使用两个循环(非嵌套(:一个用于排序,另一个用于删除项目。有没有办法在排序或类似的东西时删除重复项?对于 1000 多个项目,以下代码非常慢,我想知道是否有更快的方法:
var list = [
{a: "Somename", b: "b", c: 10},
{a: "Anothername", b: "a", c: 12},
// and so on
];
function sortList(list) {
// Sort the list based on the 3 specific values
list = list.sort(function(a,b) {
a.a = a.a.toLowerCase();
b.a = b.a.toLowerCase();
if (a.a < b.a) return -1;
if (a.a > b.a) return 1;
a.b = a.b.toLowerCase();
b.b = b.b.toLowerCase();
if (a.b < b.b) return -1;
if (a.b > b.b) return 1;
if (a.c < b.c) return -1;
if (a.c > b.c) return 1;
return 0;
});
// Loop through removing duplicates
for (var a,b,i=list.length-1; i > 0; i--) {
a = list[i];
a.a = a.a.toLowerCase();
a.b = a.b.toLowerCase();
b = list[i-1];
b.a = b.a.toLowerCase();
b.b = b.b.toLowerCase();
if (a.a===b.a && a.b===b.b && a.c===b.c) list.splice(i-1,1);
}
return list;
}
list = sortList(list);
请不要使用jQuery答案,或建议使用其他库的答案。导入库来做这么简单的事情似乎有点矫枉过正。
不要在第二个循环中使用splice
,只需将唯一值放在新数组中即可。因此,在每次迭代中,您只需将值附加到新数组,而不是splice
现有数组; push
比splice
快得多.例如:
var newList = [];
if (list) {
newList.push(list[0];
for (var a,b,i=1; i < list.length; i++) {
a = list[i];
a.a = a.a.toLowerCase();
a.b = a.b.toLowerCase();
b = list[i-1];
b.a = b.a.toLowerCase();
b.b = b.b.toLowerCase();
if (a.a!==b.a || a.b!==b.b && a.c!==b.c) {
newList.push(a);
}
}
}
在搜索了周围之后,我想出了自己的答案。它仍然使用这两个循环,但这比排序然后删除重复项要快得多,并且比在排序时跟踪重复项更一致。基本上,我遍历数组,使用我想要排序/删除重复项的属性值('key'(的串联将每个项目保存在一个对象中。如果密钥已经存储,我知道它是重复的,可以继续下一个。否则,我将项目添加到新数组中。删除重复项后,我对数组进行排序并返回:
function sortList(list){
// Since the 3 properties I'm sorting by can be converted to string I can loop
// through the specified array, creating 'keys' by concating those property values
// and adding those 'keys' to a temporary object. If the 'key' is present in the
// temporary object, I know it's a duplicate in the array and can ignore it.
// Otherwise I know it's not a duplicate and add it to a new array
for (var a=0,b=list.length,item,key="",dupObj={},nList=[]; a<b; a++) {
item=list[a];
key=item.a.toLowerCase()+"_"+item.b.toLowerCase()+"_"+item.c;
if(!dupObj[key]) {
dupObj[key]=true;
nList.push(item);
}
}
// Now that we have an array without duplicates we can sort the array:
nList.sort(function(a,b) {
if ((a.a = a.a.toLowerCase()) < (b.a = b.a.toLowerCase()) return -1;
if (a.a > b.a) return 1;
if ((a.b = a.b.toLowerCase()) < (b.b = b.b.toLowerCase())) return -1;
if (a.b > b.b) return 1;
if (a.c < b.c) return -1;
if (a.c > b.c) return 1;
return 0;
});
// return the new list
return nList;
}
var list = [
{a: "Somename", b: "b", c: 10},
{a: "Anothername", b: "a", c: 12},
// and so on
];
list = sortList(list);
相关文章:
- AngularJS:删除重复的对象并按值对数组进行重新排序
- 在对数组进行排序时删除重复项
- jQueryUI可排序-从可排序列表中删除li
- Magento网站-按选项排序:上下箭头与排序数据绑定,只需删除上下箭头即可
- 无法从jQuery可排序列表中删除新添加的项
- 删除后的剑道网格排序
- JQuery UI:将一个项从非可排序项的可排序项中删除
- CKEDITOR内联在动态创建的元素上(可删除/可排序)
- JSON 合并、数组推送和排序和删除双精度
- 在不删除/附加 DOM 节点的情况下对 DOM 节点进行重新排序
- 如何选择性地禁用可排序的 jQuery UI 的删除
- 如何删除项目的可排序 JQuery
- 删除数组中重复的未排序对象
- Jquery UI 可排序和可删除 - 无法将项目从一个列表克隆到另一个列表
- 删除动态元素并对这些元素的值和 ID 重新排序
- 如何删除空格和重新排序单词
- 如何单击时从排序表标题中删除
- Knockout JS + JQuery UI - 在重新排序后从可观察数组中删除项目的问题
- 如何在删除之前设置 jquery 可排序目标列表的样式
- 如何在angularjs中删除排序时重复的值