【发布时间】:2021-02-09 09:31:34
【问题描述】:
这是我的主要数据:(this.data)
{
"id": 7,
"name": "Revenue",
"characterCount": 1,
"maxLevel": 4,
"individualCode": "1",
"combinedCode": "02-1",
"isActive": true,
"topLevelId": 2,
"topLevelChildCount": 1,
"parentId": 2,
},
{
"id": 8,
"name": "Revenue1",
"characterCount": 1,
"maxLevel": 4,
"individualCode": "1",
"combinedCode": "02-1",
"isActive": true,
"topLevelId": 2,
"topLevelChildCount": 1,
"parentId": 2,
},
我有一个子数据:this.childData
{
"id": 14,
"name": "Sales",
"characterCount": 1,
"maxLevel": 2,
"individualCode": "1",
"combinedCode": "02-1-1",
"isActive": true,
"topLevelId": 2,
"topLevelChildCount": 0,
"parentId": 7
},
{
"id": 15,
"name": "Sales1",
"characterCount": 1,
"maxLevel": 2,
"individualCode": "1",
"combinedCode": "02-1-1",
"isActive": true,
"topLevelId": 2,
"topLevelChildCount": 0,
"parentId": 7
}
我想将这个 childData 推送到 main this.data 中的特定索引处,例如索引 0,键为“child”
表示我的数据应如下所示:
{
"id": 7,
"name": "Revenue",
"characterCount": 1,
"maxLevel": 4,
"individualCode": "1",
"combinedCode": "02-1",
"isActive": true,
"topLevelId": 2,
"topLevelChildCount": 1,
"parentId": 2,
"child": [
{
"id": 14,
"name": "Sales",
"characterCount": 1,
"maxLevel": 2,
"individualCode": "1",
"combinedCode": "02-1-1",
"isActive": true,
"topLevelId": 2,
"topLevelChildCount": 0,
"parentId": 7
},
{
"id": 15,
"name": "Sales1",
"characterCount": 1,
"maxLevel": 2,
"individualCode": "1",
"combinedCode": "02-1-1",
"isActive": true,
"topLevelId": 2,
"topLevelChildCount": 0,
"parentId": 7
}
]
},
{
"id": 8,
"name": "Revenue1",
"characterCount": 1,
"maxLevel": 4,
"individualCode": "1",
"combinedCode": "02-1",
"isActive": true,
"topLevelId": 2,
"topLevelChildCount": 1,
"parentId": 2,
},
}
我试过这个 this.data[index].push(this.childData);
但对我没有用。有什么方法可以实现吗?
getChildData(id,index)
{
console.log('id',id);
console.log('index',index);
const params ={};
params['parentId'] = id;
this.jvLedgerReportService.getMonthlyActivityReport(params).subscribe(data => {
this.childData = data.items
console.log("childData",this.childData);
this.data[index].push(this.childData);
console.log("after",this.data);
})
}
【问题讨论】:
-
您是否同时拥有不同数组中的数据,并且您想根据 ID 合并它们?如果我错了,请纠正我
-
是的。我已经添加了方法
-
您可以创建一个界面,然后简单地执行以下操作:
this.data['child']=this.childData -
@at-in 我必须在我得到的特定索引处添加它
-
@ashish 看看我的答案。
标签: javascript angular typescript