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?
1) Using jQuery If you are someone strongly committed to using the jQuery library, you can use the . inArray( ) method. If the function finds the value, it returns the index position of the value and -1 if it doesn't.
inArray( value, array [, fromIndex ] )Returns: Number. Description: Search for a specified value within an array and return its index (or -1 if not found).
jQuery :contains() SelectorThe :contains() selector selects elements containing the specified string. The string can be contained directly in the element as text, or in a child element. This is mostly used together with another selector to select the elements containing the text in a group (like in the example above).
PHP in_array() FunctionThe in_array() function searches an array for a specific value. Note: If the search parameter is a string and the type parameter is set to TRUE, the search is case-sensitive.
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; }); 
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With