I want to use Object.assign
overwrite a nested object within an object.
for example if I have an object like
let obj = {2: {fruit: "orange", animal: "dog"} 3: {fruit: "apple", animal: "cat"}}
How would I use Object.assign
to update for example the second object with key 3
to {fruit: "banana", animal: "pig"}
but still maintain the structure of obj
?
This solution should be dynamic so if for example the next time, the first object with key 2
could be updated to {fruit: "strawberry", animal: "bat"}
EDIT
So the new returned object will be
{2: {fruit: "orange", animal: "dog"} 3: {fruit: "banana", animal: "pig"}}
EDIT2
Solution needs to be dynamic
EDIT3
Seems as though this can't be achieved with just Object.assign
so is there a deep copy solution to do this?