2014-09-30 56 views
0

數組中刪除specifc值我有一個數組無法從使用jQuery

,我試圖從陣列

刪除specfic元素我裏面試過這樣

var existingLabels = [1, 2, 3, 2, 2, 4]; 


var loc_name = 1; 

existingLabels = $.grep(existingLabels, function(loc_name) { 
    return loc_name != loc_name; 
}); 

alert(existingLabels); 
+0

您是試圖移除第一個數組元素還是數組元素值爲'1'?在這個例子中顯然他們是一樣的,這就是爲什麼我需要澄清。 – j08691 2014-09-30 14:31:07

+0

我正嘗試刪除已獲得值1的lement。 – Pawan 2014-09-30 14:31:56

+0

那麼這是怎麼樣的從http://stackoverflow.com/questions/3596089/how-to-remove-specifc-value-from-array-using-jquery? – j08691 2014-09-30 14:32:49

回答

0

比較條件grep回調沒有意義,因爲不幸的變量名稱:

return loc_name != loc_name; // always false 

已更正的腳本:

existingLabels = $.grep(existingLabels, function(el) { 
    return el != loc_name; 
});