I currently have a method to search some product data which looks similar to this
function search(title) {
var result = [];
for (i = 0; i < data.length; i++) {
if (data[i].title.indexOf(title) > -1) {
result.push(data[i]);
}
}
return result;
}
Which works when trying to search my data for a product whose title contains the title being searched for. The title is passed in via an input.
However, I would to be able to perform a search for multiple properties and not just the title.
So, for example, say If I have 4 inputs like title, id, location, price and you can search for as many or as few of these as you like. So you can search for one at a time, two at time etc...
How should I approach this without having to write a completely new function? Would it be something similar like
function search(params) {
var result = [];
for (i = 0; i < data.length; i++) {
if (look for all params being searched for here) {
result.push(data[i]);
}
}
return result;
}
Edit: Just to show how I would pass the data
View
<input ng-model="params.one">
<input ng-model="params.two">
<input ng-model="params.three">
<input ng-model="params.four">
Controller
$scope.search = function() {
var params = $scope.params;
// call search service which uses the above function...
}