2012-08-01 90 views
0

我想要得到的項目列表中的一個頁面,並將其推到一個數組:jQuery的陣列中的多個項目

$('#softwareUpdates article').each(function(index) {  
    productList.push({ 
     class: $(this).attr("class"), 
     text: $(this).attr("su_title") 
    }); 
}); 

不過,我想避免多個項目,所以當我目前的檢查productList的陣列我有:

Item 1, Item1, Item 2, Item 3, Item 3, Item 4 

我想什麼有是:

Item 1, Item 2, Item 3, Item 4 
+0

試試這個:http://stackoverflow.com/questions/2822962/jquery-remove-duplicate-elements – 2012-08-01 15:08:41

+1

你可以嘗試先使用'.filter()'。 – NicoSantangelo 2012-08-01 15:09:27

回答

0

我把下面的代碼從http://hackingon.net/post/Handy-Javascript-array-Extensions-e28093-distinct().aspx

Array.prototype.distinct = function() { 
    var derivedArray = []; 
    for (var i = 0; i < this.length; i += 1) { 
    if (!derivedArray.contains(this[i])) { 
     derivedArray.push(this[i]) 
    } 
    } 
    return derivedArray; 
}; 

也有相當於.NET的Linq的JavaScript庫。任何這些應該工作。這裏有一個:http://jsinq.codeplex.com/

+0

這工作,但我的數組充滿了對象,有沒有辦法實現與對象相同? – paulcripps 2012-08-01 19:04:12

+0

這就是爲什麼我建議用於JavaScript的Linq克隆。他們讓你從對象中挑選出值,然後在它們上面應用獨特的值。 – 2012-08-01 21:24:33