Any way to use immutable.js with lodash? Any way to use immutable.js with lodash? reactjs reactjs

Any way to use immutable.js with lodash?


You could use Ramda if you want immutability and side-effect free functions.

The library provides useful functions similar to lodash but in a functional programming style that never mutates user data.

Consider this React example using the flatten function in Ramda using ES6 syntax.

import { flatten } from 'ramda';  const users = ['Steve Jobs', ['Steve Wozniak']];const flatUsers = flatten(users);// returns ['Steve Jobs', 'Steve Wozniak']


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 stuffmapped_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.


I've recently written a Lodash wrapper providing Immutable.JS support called mudash. Most of the major Lodash functions are supported with more being added regularly.