How can I clone a tree structure of an array of objects to another one, having different or less attibutes on the cloned one in TypeScript? For example on this:
[
{
"name":"root_1",
"extradata":"whatever",
"anArray":[
{ "id":1, "value":"test" },
{ "id":1, "value":"test" }
],
"anObject":{ "id":1, "value":"test" },
"children":[
{
"name":"child_1",
"extradata":"whatever",
"anArray":[],
"anObject":{},
"children":[
{
"name":"root_1",
"extradata":"whatever",
"anArray":[],
"anObject":{},
"children":[]
},
{
"name":"root_1",
"extradata":"whatever",
"anArray":[],
"anObject":{},
"children":[]
}
]
}
]
},
{
"name":"root_2",
"extradata":"whatever",
"anArray":[],
"anObject":{},
"children":[]
}
]
I need a clone of it but instead of "name" the atribute "label", instad of "extradata" = "key" and without the "anArray" and "anObject". Like this:
[
{
"label":"root_1",
"key":"whatever",
"children":[
{
"label":"child_1",
"key":"whatever",
"children":[
{
"label":"root_1",
"key":"whatever",
"children":[]
},
{
"label":"root_1",
"key":"whatever",
"children":[]
}
]
}
]
},
{
"label":"root_2",
"key":"whatever",
"children":[]
}
]
I need to clone it, to use it the data format for the primeng tree component.
I am trying with something like
this.myTree = tree.map((m, index) => Object.assign({}, {
label: m.name,
key: m.extradata,
children: m.children
}));
but the children are not following the same shape.