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);
4
  • 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. Commented Sep 30, 2014 at 14:31
  • I am trying to remove the lement which has got value 1 . Commented Sep 30, 2014 at 14:31
  • So how's this different from stackoverflow.com/questions/3596089/…? Commented Sep 30, 2014 at 14:32
  • @j08691 The same, however OP's problem is that he picked unfortunate name for current iteration element inside grep callback. Commented Sep 30, 2014 at 14:34

1 Answer 1

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;
});
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.