55

Can jQuery test an array for the presence of an object (either as part of the core functionality or via an avaible plugin)?

Also, I'm looking for something like array.remove, which would remove a given object from an array. Can jQuery handle this for me?

Costique
  • 23,712
  • 4
  • 76
  • 79
morgancodes
  • 25,055
  • 38
  • 135
  • 187

4 Answers4

92

jQuery.inArray returns the first index that matches the item you searched for or -1 if it is not found:

if($.inArray(valueToMatch, theArray) > -1) alert("it's in there");

You shouldn't need an array.remove. Use splice:

theArray.splice(startRemovingAtThisIndex, numberOfItemsToRemove);

Or, you can perform a "remove" using the jQuery.grep util:

var valueToRemove = 'someval';
theArray = $.grep(theArray, function(val) { return val != valueToRemove; });
Prestaul
  • 83,552
  • 10
  • 84
  • 84
  • Thanks Prestaul. Thing is, I don't know the index of the object I need to remove, so I think I still need array.remove. – morgancodes Jan 16 '09 at 15:28
  • oh, cool. Right there in the docs under "utilities". "RTFM" would have been an acceptalbe answer:) thanks for pointing me there though. I think jquery.grep may give me the remove functionality I need. – morgancodes Jan 16 '09 at 15:33
  • @morgancodes, it will indeed! I was adding that to my answer as you were commenting. – Prestaul Jan 16 '09 at 15:36
  • 4
    also: theArray.splice($.inArray(value, theArray), 1); – rfunduk Jan 16 '09 at 16:51
  • @thenduks, I avoided that because it only works if your array contents are all unique. It only removes the first match. – Prestaul Jan 17 '09 at 03:25
  • 1
    BTW: "theArray.splice($.inArray(value, theArray), 1);" only works if the item is in the list. – Amir Jan 22 '10 at 00:36
3

If your list contains a list of elements, then you can use jQuery.not or jQuery.filter to do your "array.remove". (Answer added because of the high google score of your original question).

Bryan Larsen
  • 9,468
  • 8
  • 56
  • 46
  • "(Answer added because of the high google score of your original question)" ??!! - What about "Answer added because I want to help" ?? – Jonathan Mar 27 '12 at 14:02
  • I don't think I'm answering the original question, but it's information that might be useful to somebody whose searches land here. – Bryan Larsen Mar 27 '12 at 19:13
0

This is not jQuery, but in one line you can add a handy 'contains' method to arrays. I find this helps with readability (especially for python folk).

Array.prototype.contains = function(a){ return this.indexOf(a) != -1 }

example usage

 > var a = [1,2,3]
 > a.contains(1)
true
 > a.contains(4)
false

Similarly for remove

Array.prototype.remove = function(a){if (this.contains(a)){ this.splice(this.indexOf(a),1)}; return this}

> var a = [1,2,3]
> a.remove(2)
[1,3]

Or, if you want it to return the thing removed rather than the altered array, then

Array.prototype.remove = function(a){if (this.contains(a)){ return this.splice(this.indexOf(a),1)}}

> var a = [1,2,3]
> a.remove(2)
[2]
> a
[1,3]
user2013483
  • 71
  • 2
  • 2
0

I found way to remove object:

foot = { bar : 'test'};
delete foot[bar];
Liutas
  • 5,547
  • 4
  • 23
  • 22