首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用key将对象数组推入数组

如何使用key将对象数组推入数组
EN

Stack Overflow用户
提问于 2020-10-27 13:31:12
回答 6查看 101关注 0票数 0

这是我的主要数据:(this.data)

代码语言:javascript
复制
    {
                    "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

代码语言:javascript
复制
{
                "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        
            }

我想在主this.data中以特定的索引推送此childData,例如索引0,关键字为“this.data”

意味着我的数据应该是这样的:

代码语言:javascript
复制
{
                    "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.dataindex.push(this.childData);

但对我不起作用。有什么方法可以让我做到这一点吗?

代码语言:javascript
复制
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);
      })
    }
EN

回答 6

Stack Overflow用户

发布于 2020-10-27 13:38:45

代码语言:javascript
复制
this.data[index]['child'] = this.childData;
   OR
this.data[index].child = this.childData;
票数 4
EN

Stack Overflow用户

发布于 2020-10-27 13:43:11

创建一个" child“数组键,并将子数据推送到其中

代码语言:javascript
复制
var parent = [];

parent.push({
    "id": 7,
    "name": "Revenue",
    "characterCount": 1,
    "maxLevel": 4,
    "individualCode": "1",
    "combinedCode": "02-1",
    "isActive": true,
    "topLevelId": 2,
    "topLevelChildCount": 1,
    "parentId": 2,
    "child":[]
});
    parent.push({
        "id": 8,
        "name": "Revenue1",
        "characterCount": 1,
        "maxLevel": 4,
        "individualCode": "1",
        "combinedCode": "02-1",
        "isActive": true,
        "topLevelId": 2,
        "topLevelChildCount": 1,
        "parentId": 2,
        "child":[]
});

var 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        
}];

parent[0].child.push(childData);

console.log(parent); 
票数 0
EN

Stack Overflow用户

发布于 2020-10-27 13:46:13

代码语言:javascript
复制
var parent = [{
                    "id": 7,
                    "name": "Revenue",
                    "parentId": 2,
                },
              {
                    "id": 8,
                    "name": "Revenue1",
                    "parentId": 2,
                }];

var child = [{
                    "id": 11,
                    "name": "Revenue",
                    "parentId": 7,
                },
                {
                    "id": 81,
                    "name": "Revenue1",
                    "parentId": 7,
                }];

var result = parent.map(x=> {
  var childData = child.filter(y=>y.parentId==x.id);
  debugger;
  if (childData && childData.length >0) {
    x['child'] = childData;
  }
  return x;
});

console.log(result);

循环通过父记录,并将ID与子记录进行匹配。如果找到记录,则将其作为Child属性添加到您的主记录中,并存储数据。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64548762

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档