Created
June 26, 2021 23:18
-
-
Save siddharth-sunchu/537ddd15ba5071a2f87a0953fad449e6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const employee = { | |
name: 'Siddharth', | |
age: 30, | |
salary: { | |
annual: '100K', | |
hourly: '$50' | |
} | |
}; | |
const copyOfEmployee = JSON.parse(JSON.stringify(employee)); // => DEEP COPY | |
console.log('employee => ', employee); | |
console.log('copyOfEmployee => ', copyOfEmployee); | |
/* | |
employee => { | |
name: 'Siddharth', | |
age: 35, | |
salary: { annual: '100K', hourly: '$50' } | |
} | |
copyOfEmployee => { | |
name: 'Siddharth', | |
age: 35, | |
salary: { annual: '100K', hourly: '$50' } | |
} | |
*/ | |
console.log('------------After Modification-----------'); | |
copyOfEmployee.name = 'Elon Musk'; | |
copyOfEmployee.salary.annual = '120K'; | |
console.log('employee => ', employee); | |
console.log('copyOfEmployee => ', copyOfEmployee); | |
/* | |
------------After Modification----------- | |
employee => { | |
name: 'Siddharth', | |
age: 30, | |
salary: { annual: '100K', hourly: '$50' } | |
} | |
copyOfEmployee => { | |
name: 'Elon Musk', | |
age: 30, | |
salary: { annual: '120K', hourly: '$50' } | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment