I’m trying to use the jQuery $.inArray function to iterate through an array and if there’s an element whose text contains a particular keyword, remove that element. $.inArray is only returning the array index though if the element’s text is equal to the keyword.
For example given the following array named ‘tokens’:
- tokens {...} Object
[0] "Starbucks^25^http://somelink" String
[1] "McDonalds^34^" String
[2] "BurgerKing^31^https://www.somewhere.com" String
And a call to removeElement(tokens, 'McDonalds'); would return the following array:
- tokens {...} Object
[0] "Starbucks^25^http://somelink" String
[1] "BurgerKing^31^https://www.somewhere.com" String
I’m guessing this may be possible using the jQuery $.grep or $.each function, or maybe regex. However, I’m not familiar enough with jQuery to accomplish this.
Any help would be appreciated!
grepis indeed the way to go.This looks for the keyword as a substring. If the elements of the array have a particular format (which they appear to, but it isn’t stated for certain), alter the test to match the format.
Note the first ‘^’ is a meta-character matching the beginning of the string; the second simply matches a ‘^’ character.