ksumarine
ksumarine

Reputation: 782

Remove array items with RegExp?

I have a bunch of links that are used to filter information, see: https://i.sstatic.net/JHxPL.png

When you click on a link, I add it to an array. For example:

Venue>All = venue-0, Venue>Home = venue-1, Venue>Away = venue-2, 
Vs>All = vs-0, Vs>Lefties = vs-1, Vs>Righties = vs-2
 etc, etc, etc.

If the user clicks "All" in any category, I want to search through the array and remove any items that contain that category. For example: myArray['venue-1','venue-2','vs-1']...click Venue>all, remove 'venue-1' and 'venue-2' leaving myArray['vs-1'].

Can I do this using a regular expression like: /^venue/ and changing the word depending on which "All" you click?

I know I need to do a combination of $.inArray or indexOf() and splice() but I can't get it working.

Upvotes: 2

Views: 1039

Answers (3)

Phrogz
Phrogz

Reputation: 303361

For a non-jQuery solution that mutates the array in-place:

for (var i=myArray.length;i--;){
  if (...) myArray.splice(i,1);
}

Where ... is whatever test you want, such as:

if (myRegeEx.test(myArray[i]))

Upvotes: 0

robrich
robrich

Reputation: 13205

Try this:

var newList = $.filter(function () {
    return this.indexOf('venue') != 0;
});

Upvotes: 0

Tejs
Tejs

Reputation: 41256

You can do this simply with the $.grep utility:

var someArray = [1, 2, 3];

var numbersGreaterThanOne = $.grep(someArray, function(item)
{
    return item > 1;
});

This is just a sample, but you can perform whatever callback to filter elements that you like.

Upvotes: 1

Related Questions