Searching for data
Applications don't use entire collections. Rather, they iterate over a collection subset, or they look for a specific item in the collection. Lo-Dash has a number of functional tools to help the programmer find the data they need.
Filtering collections
The simplest way to perform a filter operation on a collection using Lo-Dash is to use the where()
function. This function takes an object argument and will match its properties against each item in the collection, as shown in the following code:
var collection = [ { name: 'Moe', age: 47, gender: 'm' }, { name: 'Sarah', age: 32, gender: 'f' }, { name: 'Melissa', age: 32, gender: 'f' }, { name: 'Dave', age: 32, gender: 'm' } ]; _.where(collection, { age: 32, gender: 'f' }); // → // ...
Get Lo-Dash Essentials now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.