Since Objects in JavaScript are reference values, you cant just Copy Using the =. But no worries, here are 3 different ways for you to Clone an Object
- Spread
- Object.assign
- JSON
const food = { beef: '๐ฅฉ', bacon: '๐ฅ' }
// "Spread"
{ ...food }
// "Object.assign"
Object.assign({}, food)
// "JSON"
JSON.parse(JSON.stringify(food))
// RESULT:
// { beef: '๐ฅฉ', bacon: '๐ฅ' }
Top comments (0)