0

我想获取页面中的项目列表并将它们推送到数组中:

$('#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
4

1 回答 1

0

I pulled the following code from 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;
};

There are also javascript libraries equivalent to .NET's Linq. Any of those should work. Here's one: http://jsinq.codeplex.com/

于 2012-08-01T15:13:04.907 回答