I have an array structured like this and I'm trying to get a copy of it (to modify and use for React setState()). In Python I can use copy.deepcopy() but I can't find an easy way to do this in JavaScript.
notes=[
{
contents: "Hello World 1",
function: console.log,
children: [
{
contents: "Hello World A",
function: console.log,
children: []
},
]
},
{
contents: "Hello World 2",
function: console.log,
children: []
}
]
I found this article and similar solutions on stackoverflow, but none of them work for me. https://medium.com/@Farzad_YZ/3-ways-to-clone-objects-in-javascript-f752d148054d Two solutions are only a shallow copy, and JSON.parse doesn't work on functions.
I'd like to have a function that can deep copy any array or object containing any arbitrary structure of nested JavaScript datatypes.
I'd rather not reinvent the wheel writing a complex recursive function to traverse and clone everything, is there any existing solution?