Glen Swift Glen Swift - 9 months ago 135
Javascript Question

Any way to use immutable.js with lodash?

I'm using immutable.js with my flux application. It is very useful and gives performance boost. But what actually makes me sad is the fact that I can't use lodash together with it. Lodash provides great API with ton of useful functions, so I wonder maybe there is a way to get them together to work for me?


I'm assuming you're trying to do something like do a lodash map on an immutable set. Lodash doesn't do helpful things when you try to do that directly.

Note that immutable.js already has a lot of its own manipulation functions (like map) as well as its own lazy chaining (via Seq) so you should look into those.

If you need to do something that lodash provides and immutable.js does not, one thing you can do is take your immutable object and convert it to a vanilla JS object for consumption by lodash. For instance:

// Do all of your fancy immutable.js stuff...
my_set = immutable.Set([1,2,3]).union(immutable.Set([2,3,4]))
// ...and then convert to JS before you do all of your fancy lodash stuff
mapped_set = _(my_set.toArray()).map(whatever)

You'll of course have to take performance into account here, since you may end up with the worst of both worlds if you convert from one to the other by copying your data into a vanilla data structure. In the toy case above, for instance, you'd probably be better off using an immutable.js map() directly.