Updating Deep Immutable Object
let newVersion = {
language: 'Default Language', // Set default for unspecified properties
...previousVersion,
name: 'New Name', // Override the name property
address: {
...previousVersion.address,
zipCode: '99999' // Update nested zip code
},
items: [
{title: 'New First Item'}, // Add an item to the beginning of items
...previousVersion.items,
{title: 'New Last Item'} // Add an item to the end of items
],
};
@jonearley what about a .map call on previousVersion.items ? If you find the item that you want to change, replace it with a new one, else just return each item.
e.g.
items: previousVersion.items.map((item) => (isItemToChange ? newItem : item));