我有一個arrayList與其中的對象。如果對象具有相同的值,我只需要顯示一個值。如何從arrayList中刪除重複的對象值jquery/Javascript
例如:[{a:0,b:1},{a:1,b:0},{a:0,b:1}] 從上面的例子中,我只需要顯示第一個和第二個對象,並跳過第三個對象,因爲它與第一個對象相同。
注意:數組中的對象可以是無限的,我不能硬編碼索引值。任何人都可以幫我解決這個通用解決方案
這是我曾嘗試:
points = [];
newarr = [];
locArray = [ {a:0,b:1},{a:1,b:0},{a:0,b:1} ];
if(abc!=null){
removeDuplicateCoordinates();
$.each(newarr,function(key,val){
points.push([val.a,val.b]);
});
}
function removeDuplicateCoordinates(){
var arr = locArray;
$.each(arr, function(index,item){
if(searchForItem(newarr,item)<0){
newarr.push(item);
}
});
}
function searchForItem(array, item){
var i, j, current;
for(i = 0; i < array.length; ++i){
if(item.length === array[i].length){
current = array[i];
for(j = 0; j < item.length && item[j] === current[j]; ++j);
if(j === item.length)
return i;
}
}
return -1;
}
看看這些解決方案:http://jsperf.com/remove-duplicates-from-an-array-of-strings-javascript,http://jsperf.com /去重-INT陣列。希望你能輕鬆採納它們。如果不是,請選擇一個您喜歡的,我們將嘗試實施它。 –
@AlexanderArutinyants:您能否實施我的結構?我很難理解您提供的參考鏈接。 –
你應該保留源數組嗎? –