0

I want to get a list of items in a page and push them into an array:

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

HOWEVER I want to avoid multiple items, so when I current check the productList array I have:

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

What I would like to have is:

  Item 1, Item 2, Item 3, Item 4
paulcripps
  • 157
  • 7

1 Answers1

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/

John Fisher
  • 22,355
  • 2
  • 39
  • 64
  • This worked but my array is full of objects, is there a way to achieve the same with objects? – paulcripps Aug 01 '12 at 19:04
  • That's why I suggested the Linq clones for JavaScript. They let you pick and choose values out of an object, then apply the distinct over them. – John Fisher Aug 01 '12 at 21:24