Yikes, didn't realize lodash already supported all of that! I did a quick search through the documentation, but didn't see that functionality listed 😬 my bad, thanks for setting the record straight!
Full-time web dev; JS lover since 2002; CSS fanatic. #CSSIsAwesome
I try to stay up with new web platform features. Web feature you don't understand? Tell me! I'll write an article!
He/him
Yeah tbh it's slightly hidden in the documentation itself, but the relevant documentation is here. There's a big paragraph there that discusses the difference between _(foo) and _.chain(foo), but the main point is you get an object that lets you chain the usual lodash methods and call .value() when you want the final result.
The basic difference between those two is that _(foo) expects an array or an object, and will automatically drop out of the chain and return the final result value as soon as you call a method that's not guaranteed to return an object or array. So if you call _(foo).map().filter().sortBy(), you'll need .value() at the end, but if you add a .get() or a .reduce(), it will return the value immediately, which can be nice. _.chain(foo) will never drop out of the chain object, you always need to call .value(), which is also helpful sometimes.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Yikes, didn't realize lodash already supported all of that! I did a quick search through the documentation, but didn't see that functionality listed 😬 my bad, thanks for setting the record straight!
Yeah tbh it's slightly hidden in the documentation itself, but the relevant documentation is here. There's a big paragraph there that discusses the difference between
_(foo)
and_.chain(foo)
, but the main point is you get an object that lets you chain the usual lodash methods and call.value()
when you want the final result.The basic difference between those two is that
_(foo)
expects an array or an object, and will automatically drop out of the chain and return the final result value as soon as you call a method that's not guaranteed to return an object or array. So if you call_(foo).map().filter().sortBy()
, you'll need.value()
at the end, but if you add a.get()
or a.reduce()
, it will return the value immediately, which can be nice._.chain(foo)
will never drop out of the chain object, you always need to call.value()
, which is also helpful sometimes.