javascript - Lodash _.filter function must only meet ONE condition -
i using lodash _.filter() function filter objects meet specific condition based on user search, either street address, town or country.
i have extracted search string , want filter against several values within each object , must return object if string found in of keys.
to explain want use example website.
var users = [ { 'user': 'barney', 'age': 36, 'active': true }, { 'user': 'fred', 'age': 40, 'active': false }, { 'user': 'pebbles', 'age': 1, 'active': true } ]; // using `_.matches` callback shorthand _.result(_.find(users, { 'age': 1, 'active': true }), 'user');
in example, how filter users either 36 years of age or active?
according documentation seems both conditions needs met, per above example object returned.
you can pass function _.filter
:
_.filter(users, function(user) { return user.age === 36 || user.active; });
and can use same technique _.find
:
_.result(_.find(users, function(user) { return user.age === 36 || user.active; }), 'user');
Comments
Post a Comment