I have the following object in js :
let user = {name:"David",age:"23",address:{city:"Toronto",country:"Canada"}}
And, also, defined the following map object:
let map = new Map()
How can i have a deep cloned object inside of the map?
I have the following object in js :
let user = {name:"David",age:"23",address:{city:"Toronto",country:"Canada"}}
And, also, defined the following map object:
let map = new Map()
How can i have a deep cloned object inside of the map?
3
Answers
In this case, i think using
Object.assign
would be sufficient. This is because when you assign the shallow clone to the map, it creates a new reference for the address object.But if you really need a deep clone, as suggested:
Additionally, Map object are used to make retrieving an item easier. For example, getting the user by the userName. But in your case, this would be problematic in case you have multiple users with the same name.
A very easy solution is to convert it into JSON and back:
What about copying the user object before transforming it into the Map?