-4
var arr = ["Chandelier", "Big Girls Cry", "Burn the Pages", "Eye of the Needle", "Hostage", "Straight for the Knife", "Fair Game", "Elastic Heart", "Free the Animal", "Fire Meet Gasoline", "Cellophane", "Dressed In Black", "Chandelier", "Elastic Heart", "Chandelier", "Chandelier", "Elastic Heart", "Elastic Heart", "Big Girls Cry", "Big Girls Cry"];

 $.each(arr, function(i,obj){
console.log(obj);
});

How can I make sure my array is unique?

Nichole A. Miler
  • 1,281
  • 2
  • 11
  • 21

1 Answers1

1

With Array#filter() and a temporary object.

var arr = ["Chandelier", "Big Girls Cry", "Burn the Pages", "Eye of the Needle", "Hostage", "Straight for the Knife", "Fair Game", "Elastic Heart", "Free the Animal", "Fire Meet Gasoline", "Cellophane", "Dressed In Black", "Chandelier", "Elastic Heart", "Chandelier", "Chandelier", "Elastic Heart", "Elastic Heart", "Big Girls Cry", "Big Girls Cry"],
    unique = arr.filter(function (a) {
        if (!this[a]) {
            this[a] = true;
            return true;
        }
    }, {});    
    
document.write('<pre>' + JSON.stringify(unique, 0, 4) + '</pre>');
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392