Updating objects

This kind of method can also be applied to more common requirements, such as modifying an object. This is a very good idea for, say, Redux users: a reducer can be programmed that will receive the old state as a parameter and produce an updated version with the minimum needed changes, without altering the original state in any way.

Imagine you had an object as follows:

myObj = {    a: ...,    b: ...,    c: ...,    d: {        e: ...,        f: ...,        g: {            h: ...,            i: ...        }    }};

If you wanted to modify myObj.d.f, and wanted to do it in a persistent way, you would create a new object, which would have several attributes in common with the previous object, but would define new ones for the modified ones. See figure 10.6:

Figure 10.6. A persistent way of ...

Get Mastering Javascript Functional Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.