views:

117

answers:

3

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!

A: 

edit oops I totally misunderstood the question.

Well $.grep takes an "invert" flag, so you can use it directly:

var pattern = /\bword\b/;
var newArray = $.grep(oldArray, function(e) { return pattern.test(e); }, true);

Of course, now that I think about it, I could save 5 characters:

var newArray = $.grep(oldArray, function(e) { return !pattern.test(e); });
Pointy
A: 

This should be clear on its own. Removes all elements from the tokens parameter which contain the string passed in in the search parameter. Using filter()

function removeElement(tokens, search) {
    var regex = new RegExp(search);
    return $(tokens).filter(function() {
        return !regex.test(this);
    }).toArray();
}

If you want you can also specify flags for the regex

e.g.

var regex = new RegExp(search, "i");

Would remove all elements which contain the search string with case ignored


$.grep version of the code

function removeElement(tokens, search) {
    var regex = new RegExp(search);
    return $.grep(tokens, function(elem, ind) {
        return regex.test(elem);
    }, true);
}
jitter
Building a new RegExp instance for each iteration seems kind-of wasteful.
Pointy
fixed already...
jitter
+4  A: 

grep is indeed the way to go.

function removeElement(array, keyword) {
    return $.grep(array, function (item, i) {
      return item.indexOf(keyword) > -1;
  }, true);
}

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.

function removeElement(array, keyword) {
    var keyRE=new RegExp('^'+keyword+'^');
    return $.grep(array, function (item, i) {
      return keyRE.test(item);
  }, true);
}

Note the first '^' is a meta-character matching the beginning of the string; the second simply matches a '^' character.

outis
I think he wants the result array to contain only things that **don't** have the keyword.
Pointy
Pointy has it right. Although if the first removeElement function you've created returned the array index instead of the element, that would work too?
YourMomzThaBomb
@YourMomzThaBomb: in the first version (actually, both), the array index is ignored. `a.indexOf(b)` returns the starting index of string `b` in string `a`, or -1 if `b` isn't a substring of `a`.
outis