0

Possible Duplicate:
Remove item from array by value | JavaScript

Is there a way, in JS or with help of jQuery, to remove element from an array with knowing just the value of the element, not it's place inside an array?

It needs to be cross-browser compatible too.

Thanks.

Community
  • 1
  • 1
CodeVirtuoso
  • 5,702
  • 12
  • 43
  • 59
  • 1
    use jquery to be sure it is cross-browser compatible – Ali Foroughi Jan 07 '12 at 10:50
  • But if you know the value, determining its index shouldn't be too complicated. Worst case scenario, you could determine the index by parsing the array and comparing the array values, with your search criteria, and once the item index has been identified, you can just remove the item at that index. – Romi Halasz Jan 07 '12 at 10:51
  • 1
    Don't forget to allow for when the value appears in the array more than once. – nnnnnn Jan 07 '12 at 10:54

1 Answers1

2

look to the example

arr = [1, 2, 3, 4, 5] // array
var removeItem = 2; // item to be removed

arr = jQuery.grep(arr, function(value) {
return value != removeItem;
});

// new array
// [1, 3, 4, 5]
Snake Eyes
  • 14,643
  • 31
  • 97
  • 188