完全刪除重複行我有一個JavaScript下面的列表。的JavaScript - 從陣列
var list = [{
ID: "2566",
NAME: "ENTERPRISE EXPENSE",
EXECUTE: "LOAD_DIMENTION",
TYPE: "PROCEDURE"
},
{
ID: "1234",
NAME: "LOAD EXPENSE FACT",
EXECUTE: "LOAD_EXPENSE_FACT",
TYPE: "STORED PROCEDURE"
},
{
ID: "RI0031",
NAME: "LOAD HEAD COUNT",
EXECUTE: "LOAD_HEADCOUNT_FACT",
TYPE: "STORED PROCEDURE"
},
{
NAME: "Not Required",
EXECUTE: "Not Required",
TYPE: "Not Required"
},
{
NAME: "Duplicate",
EXECUTE: "Duplicate",
TYPE: "Duplicate"
},
{
ID: "RI04",
NAME: "CALCULATE FAST",
EXECUTE: "FF_CALC",
TYPE: "STORED PROCEDURE"
},
{
NAME: "FORMULAS",
EXECUTE: "FF_CALC",
TYPE: "STORED PROCEDURE"
},
{
ID: "RI0031",
NAME: "LOAD HEAD COUNT",
EXECUTE: "LOAD_HEADCOUNT_FACT",
TYPE: "STORED PROCEDURE"
},
{
ID: "RI04",
NAME: "CALCULATE FAST",
EXECUTE: "FF_CALC",
TYPE: "STORED PROCEDURE"
},
{
NAME: "FORMULAS",
EXECUTE: "FF_CALC",
TYPE: "STORED PROCEDURE"
},
{
ID: "RID005",
NAME: "CALCULATE FAST GROUP",
EXECUTE: "FF_CALC",
TYPE: "STORED PROCEDURE"
},
{
NAME: "Not Required",
EXECUTE: "Not Required",
TYPE: "Not Required"
},
{
NAME: "Duplicate",
EXECUTE: "Duplicate",
TYPE: "Duplicate"
}
];
此列表有許多重複的行。我想刪除所有重複的行。
對象可以有更多的10種性質也,所以檢查每個屬性是不是解決辦法。另外,無論我上網的方式是1維陣列還是刪除特定屬性的重複。 我怎樣才能保持一個包含所有行唯一一個列表?
我能想到的,是比較JSON對象的字符串化版本。像這樣:'JSON.stringify(obj1)=== JSON.stringify(obj2)'。我猜想這個速度足夠快,但元素屬性的順序很重要。這是一個問題嗎? – Scalpweb
沒有訂單不是問題。但即使我將每一行與所有其他行進行比較,這個過程是不是會太慢?隨着列表大小的增加,複雜度將增加。 –
「善良的用戶」的答案正是我想到的。 JSON.stringify過程是比較JS中2個對象的最快方法。我會說你必須制定一些基準來檢查它是否足夠快,但它應該沒問題。 – Scalpweb