re: Using Underscore/Lodash: Yea or Nay? VIEW POST

VIEW PARENT COMMENT VIEW FULL DISCUSSION
 

Lodash actually has two different ways to do that same thing. Basic lodash has the chain object:

_([1,2,3,4])
    .map(x => x*x)
    .filter(x => x % 2 == 0)
    .map(x => String(x))
    .value()

And then there's the more recent (or modern, if you prefer) lodash/fp module, which looks a lot a like ramda:

fp.pipe(
    fp.map(x => x*x),
    fp.filter(x => x % 2 == 0),
    fp.map(x => String(x))
)([1,2,3,4])

I've heard good things about ramda as well, don't get me wrong, but it's worth knowing all your options fully ☺

And as a side note, I don't know if it makes sense to call ramda "more modern" than lodash; it's been around in some form since late 2013, around a year and a half after lodash came on the scene. IMHO, lodash is one of the most reliable, stable, best maintained projects out there. It's continually improving and being refined, as seen in the fp module. I wouldn't be too quick to dismiss it as outdated just yet

 

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.

code of conduct - report abuse