I wish to iterate over an array
of objects
, which for purposes of illustration here are Users
. I would like to filter those Users
using a search term provided and the value of each of the objects
properties.
I have come up with the following working solution, however, I am looking to see if anyone has a more elegant method?
let users = [
{
first_name: "Super",
last_name: "User"
},
{
first_name: "Admin",
last_name: "Istrator"
}
];
function filterObjects(collection, term) {
let filtered = [];
_.each(collection, obj => {
_.forOwn(obj, (value, key) => {
if (value.includes(term)) {
filtered.push(obj);
}
});
});
return filtered;
}
console.log(filterUsers(users, "Su"));
[Edit]
I make no assumptions about the structure, or the name, or quantity of any fields on the objects, therefore I do not want to use things like object.field_name === 'something'
for example.