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 so that it 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 the following object:

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

Let's assume you wanted to modify the value of the myObj.d.f attribute, but working in a persistent way. Instead of copying the full object (with the deepCopy() function that we used earlier), we could create a new object that has several attributes in common with the previous ...

Get Mastering JavaScript Functional Programming - Second Edition 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.