並べ替える必要のあるオブジェクトを含む配列があり、各配列項目の3つの特定の値に基づいて重複を削除します。現在、2つのループ(ネストされていない)を使用しています。1つは並べ替え、もう1つはアイテムの削除です。並べ替えなどで重複を削除する方法はありますか?何千ものアイテムの場合、次のコードは非常に遅く、より速い方法があるかどうか疑問に思っています。
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の回答、または別のライブラリの使用を示唆する回答はしないでください。これほど単純なことをするためにライブラリをインポートするのは少しやり過ぎのようです。