2016-03-21 14 views
-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); 
}); 

Dizilimimin benzersiz olduğundan nasıl emin olabilirim?çoğaltılmış döndürme öğesini kaldırma

+2

Vay, bu durumda $ .each kullanıyorum –

+0

@WesFoster göndermeden önce arama. –

+0

Yani, $ .each' kullanarak kopyaları kaldırmak için bir yöntem mi istiyorsunuz? –

cevap

1

Array#filter() ve geçici bir nesne ile.

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>');

+0

Her ismi almak ve bir yere itmek için $ .each kullanıyorum, bu benim durumumda nasıl yardımcı oluyor? –

+2

neden $ .each'e ihtiyacınız var? –

+0

@downvoter, neden? –

İlgili konular