Search⌘ K
AI Features

Solution Review: Shallow to Deep

Explore deep cloning techniques in JavaScript functional programming to safely mutate object properties. Understand how JSON.stringify and JSON.parse work together, the limitations of this approach with functions and undefined values, and discover libraries like lodash for reliable deep cloning.

We'll cover the following...

Solution

Node.js
const girl = {
name: 'Anna',
info: { age: 20, number: 123 }
};
const newGirl = JSON.parse(JSON.stringify(girl));
newGirl.info.age = 30;
console.log(girl.info.age)

Explanation

The above code uses the concept of deep cloning to allow safe mutation of an object property. As you can see, changing ...