Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
lodash chaining
.filter({enabled: true})
.forEach(function (trial) {
 details.devices = _(trial.details.roomSystems)
   .filter({enabled: true})
   .map(function (device) {
     return _.pick(device, ['model', 'quantity']);


I'm confused about value(). Occasionally I'll see it when using _(randomArray), like above, other times I won't. And every time I use _.chain(randomArray) I need to add value() to get the actual values. Is there a good guide I can read up on?


The lodash docs explains why this is the case, but it can be a bit obtuse to parse:

My explanation: Remember _() and _.chain() are functions that create a wrapper around arrays, collections, functions, and single values/primitives. That chaining wrapper has an interface whose purpose is to pipe output to input and eventually get some useful value. You have to unwrap that wrapper to get to the value being piped from lodash function to lodash function. value() does the unwrapping, it can be called explicitly or implicitly (the lodash function does it from the inside automatically), but it must always be called.

The _() is for implicit chaining and _.chain() is for explicit chaining. The implicit _() chain just means if any of lodash's functions return a single value or primitive it implicitly calls value() and unwraps. The explicit _.chain() means that even if a single value or primitive is returned, you must explicitly call value() at the end of the chain to unwrap things.

I have confused you here by using an implicit _() wrapper, but am required to explicitly call value() because forEach doesn't implicitly call value(). I really should be doing an explicit flow with _.chain().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.