Counting items
A variation on the previous topic—Testing truth conditions—is counting items in a collection after their values have moved through a processing chain. For example, we might need to know how many items in a collection meet the given criteria. We can get that number using a call chain.
Using length and size()
The size()
function is handy because we can call it directly on a Lo-Dash wrapper. This is the preferred way to count the resulting items in our collection after our chain runs:
var object = { first: 'Charlotte', last: 'Hall' }, array = _.range(10); _(object) .omit('first') .size(); // → 1 _(array) .drop(5) .size(); // → 5
Here, we have array
and object
. The first chain uses the size()
function to count the number of properties after ...
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.