DEV Community

Benjamin Black
Benjamin Black

Posted on

The Present of Lodash

Given the announcement of "The Future of Lodash", with Lodash joining OpenJS, I thought I'd express my ongoing love for Lodash and share one of the many reasons why it is still useful for me day-to-day.

TL;DR Merge is an unsung hero of the library.

In the absence of something like Immer, if you need to immutably update a deeply-nested object, you can do this:

const properties = { /* ... */ };

setTracks((prev) => ({
    ...prev,
    [trackId]: {
        ...prev[trackId],
        points: {
            ...prev[trackId].points,
            [pointId]: {
                ...prev[trackId].points[pointId],
                properties: {
                    ...prev[trackId].points[pointId].properties,
                    ...properties,
                }
            }
        }
    }
}));
Enter fullscreen mode Exit fullscreen mode

Or you can do this:

setTracks((prev) => 
    merge({}, prev, {[routeId]: {points: {[pointId]: {properties}}}}))
);
Enter fullscreen mode Exit fullscreen mode

Top comments (0)