2

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.

0

3 Answers 3

1

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.

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

2 Comments

So how would I do this for what I need? I tried var filterIt = $.grep(filters,function(n,i){return n=/^venue/;}); but got nothing.
return item.indexOf('venue') == -1 ? The test is up to you.
0

Try this:

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

Comments

0

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]))

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.