lodash Chaining Chaining


Example

Any lodash collection method has two syntaxes.

Without chaining:

var arr1 = [10, 15, 20, 25, 30, 15, 25, 35];

var arr2 = _.filter(arr1, function(item){ return item % 10 === 5 });
// arr2 now contains [15, 25, 15, 25, 35]

var arr3 = _.uniq(arr2);
// arr3 now contains [15, 25, 35]

var arr4 = _.map(arr3, function(item){ return item + 1 });
// arr4 now contains [16, 26, 36]

With chaining:

var arr1 = [10, 15, 20, 25, 30, 15, 25, 35];

var arr4 = _(arr1)
    .filter(function(item){ return item % 10 === 5 })
    .uniq()
    .map(function(item){ return item + 1 })
    .value();
// arr4 now contains [16, 26, 36] without creating the intermediate results.

The chaining version of this is actually more efficient, since no intermediate results are created. The expressions are evaluated lazily by the call to .values() at the end of the chain.