如何将一组对象的树结构克隆到另一个对象,在 TypeScript 中对克隆的对象具有不同或更少的属性?例如关于这个:
[
{
"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":[]
}
]
我需要它的克隆,但不是“命名”属性“标签”,而是“extradata”=“key”并且没有“anArray”和“anObject”。像这样:
[
{
"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":[]
}
]
我需要克隆它,以使用它作为primeng树组件的数据格式。
我正在尝试类似的东西
this.myTree = tree.map((m, index) => Object.assign({}, {
label: m.name,
key: m.extradata,
children: m.children
}));
但孩子们并没有遵循相同的形状。