2

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!

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I think he wants the result array to contain only things that don't have the keyword.
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: 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.
0

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);
}

1 Comment

Building a new RegExp instance for each iteration seems kind-of wasteful.
0

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); });

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.