Implicit chaining with _(arr1)
and explicit chaining with _.chain(arr1)
work in similar ways. The examples below show how they differ slighlty.
_.chain(...)
var arr1 = [10, 15, 20, 25, 30, 15, 25, 35];
var sumOfUniqueValues = _.chain(arr1)
.uniq()
.sum() // sum returns a single value
.value(); // which must be unwrapped manually with explicit chaining
// sumOfUniqueValues is now 135
_(...)
var arr1 = [10, 15, 20, 25, 30, 15, 25, 35];
var sumOfUniqueValues = _(arr1)
.uniq()
.sum(); // sum returns a single value and is automatically unwrapped
// with implicit chaining
// sumOfUniqueValues is now 135
The two behave differently when ending the chain with an operation that returns a single value: With implicit chaining, the "unwrapping" of the single value is implied. (Thus no need to call .value()
.)
(When the implicit chain ends with a collection value, you'll still need to unwrap the result with .value()
.)