Lodash - Methods
Lodash - Core Concepts: Chaining
Section titled “Lodash - Core Concepts: Chaining”Beyond individual functions, Lodash offers a powerful way to compose multiple operations together elegantly using chaining. This allows you to pass a value through a sequence of Lodash methods without needing temporary variables for intermediate results.
Lodash provides two main approaches to chaining:
1. Explicit Chaining
Section titled “1. Explicit Chaining”You start an explicit chain by calling _.chain(value). This wraps the initial value in a Lodash wrapper object. Subsequent Lodash methods called on this wrapper will operate on the wrapped value and return a new wrapper. To get the final result out of the chain, you must call the .value() method.
Syntax & Example
Section titled “Syntax & Example”const users = [ { 'user': 'barney', 'age': 36, 'active': true }, { 'user': 'fred', 'age': 40, 'active': false }, { 'user': 'pebbles', 'age': 1, 'active': true }];
const youngestActiveUserName = _.chain(users) .filter({ 'active': true }) // Filter for active users .sortBy('age') // Sort by age (ascending) .map('user') // Pluck the 'user' property .head() // Get the first element (youngest) .value(); // Extract the final value from the wrapper
console.log(youngestActiveUserName); // => 'pebbles'2. Implicit Chaining (via lodash/fp or when using _())
Section titled “2. Implicit Chaining (via lodash/fp or when using _())”When using the main Lodash build, calling _(value) (note the single underscore) often initiates implicit chaining for many methods. Methods that typically return a new array or object (like map, filter, reject) allow further chaining without needing .chain(). However, methods that return a single value (like find, sum, head, max) will immediately return that value, terminating the implicit chain.
The lodash/fp module (functional programming variant) inherently uses implicit chaining and data-last, auto-curried functions, which promotes a different style of composition.
Implicit Chaining Example (Standard Lodash)
Section titled “Implicit Chaining Example (Standard Lodash)”const users = [ { 'user': 'barney', 'age': 36, 'active': true }, { 'user': 'fred', 'age': 40, 'active': false }, { 'user': 'pebbles', 'age': 1, 'active': true }];
// Note: .head() terminates the implicit chain hereconst youngestActiveUser = _(users) .filter({ 'active': true }) .sortBy('age') .head(); // Returns the user object directly, not a wrapper
console.log(youngestActiveUser);// => { user: 'pebbles', age: 1, active: true }
// To continue chaining after .head() you'd need explicit chaining or handle the result.Benefits of Chaining
Section titled “Benefits of Chaining”-
Readability: Chains often read like a sequence of steps, making complex transformations easier to follow.
-
Conciseness: Reduces the need for intermediate variables.
-
Composability: Encourages breaking down problems into smaller, reusable steps.
Choose explicit chaining (_.chain()) when you need full control and want to ensure all steps remain within the chain until you explicitly call .value(). Implicit chaining can be more concise for sequences where intermediate steps naturally return collections.
Learn more about chaining in the official Lodash documentation.