Hello friends, I'm dealing with a JSON object:
var companies = [
{ id: 1, name: "Test Company", admin: "Test Admin" },
{ id: 2, name: "Another Company", admin: "Test Admin", country: 'Spain' },
{ id: 3, name: "New Company", admin: "Admin 4" },
{ id: 4, name: "Free Company", admin: "Jhon Miller", city: 'New York' }
];
I am in the process of creating a function to return a new JSON with elements based on specific filters passed as parameters. So far, I've created a simple function like this:
function searchItems(companies, filter) {
var result;
if (typeof filter === "undefined" || filter.length == 0) {
result = companies;
} else {
result = _.filter(companies, function(c) {
return _.includes(_.lowerCase(c.name), _.lowerCase(filter));
});
}
}
The current function only allows filtering by the company name. My question is: how can I modify it to allow filtering by name, admin, country, and city but not by ID? For example, if the filter passed is 4, the function should return:
{ id: 3, name: "New Company", admin: "Admin 4" }
Or if the filter is "iLl", it should return:
{ id: 4, name: "Free Company", admin: "Jhon Miller", city: 'New York' }
Thank you!