0

I have got an array

And i am trying to remove a specfic element from the array

I tried this way

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);
Pawan
  • 28,159
  • 84
  • 232
  • 394
  • Are you trying to remove the first array element, or the array element that has a value of `1`? Obviously they're the same in this example, which is why I need clarification. – j08691 Sep 30 '14 at 14:31
  • I am trying to remove the lement which has got value 1 . – Pawan Sep 30 '14 at 14:31
  • So how's this different from http://stackoverflow.com/questions/3596089/how-to-remove-specifc-value-from-array-using-jquery? – j08691 Sep 30 '14 at 14:32
  • @j08691 The same, however OP's problem is that he picked unfortunate name for current iteration element inside grep callback. – dfsq Sep 30 '14 at 14:34

1 Answers1

0

Comparison condition inside grep callback makes no sense because of unfortunate variable name:

return loc_name != loc_name; // always false

Corrected script:

existingLabels = $.grep(existingLabels, function(el) {
    return el != loc_name;
});
dfsq
  • 182,609
  • 24
  • 222
  • 242