3

I am trying to pass an array into a function that looks at a part of the current URL and then filters data on the page based on that URL. The array has many values a person could search for on the page.

Currently the filterFor param can only take a single string and I want it to take a array of strings that could be searched for.

function queryList(filterFor, filterClass, filterClassTwo) {
    var searchResult = $.urlParam('filter');
    if (searchResult === filterFor) {
        degreeProgram.filter(function (item) {
            if (item.values()[filterClass] === filterFor || 
                item.values()[filterClassTwo] === filterFor) {
                return true;
            } else {
                return false;
            }
        });
        $('#filterDisplay').html('<span>' + filterFor + '</span>');
        $('#filterDisplay').addClass('activated');   
    }   
}

queryList('Chemistry', 'programName');

This would allow the page to filter to show any programName with the value of chemistry. The part of the URL the function looks at is this:

?filter=Chemistry

I need it to work like this:

var myArray = ['Chemistry', 'Math' 'Earth Science'];
queryList(myArray, 'programName');

allowing me to pass in a list of programs that the query could take in.

Maytham Fahmi
  • 31,138
  • 14
  • 118
  • 137

1 Answers1

0

You should be able to accomplish this using the built in array methods. indexOf() is used to determine whether or not the array contains the search term.

function arrayExample(myArray){
  var searchTerm = "testing";
  if(myArray.indexOf(searchTerm) !== -1){
     alert("match");
  }
  else{
     alert("no match");
  }
}

arrayExample("blah","aaa","oooh");
arrayExample("testing","aaa","oooh");
kurt
  • 1,146
  • 1
  • 8
  • 18