【问题标题】:How to push object array into array with key如何使用键将对象数组推入数组
【发布时间】: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


【解决方案1】:
this.data[index]['child'] = this.childData;
   OR
this.data[index].child = this.childData;

【讨论】:

    【解决方案2】:

    创建一个“子”数组键并将子数据推送到它

    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); 
    

    【讨论】:

      【解决方案3】:

      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 与子记录匹配。如果找到记录,则将其作为子属性添加到您的主记录中并存储数据。

      【讨论】:

        【解决方案4】:
        // Solution using callbacks
        const traverse = (data, callback) => {
         const visit = (node) => {
          callback(node);
          node.child.forEach((child) => visit(child));
         }
         visit(data);
        }
        
        const add = (yourData, dataToAdd, parentId) => {
         traverse(yourData, (node) => {
          if (node.id === parentId) {
           if (!node.child) {
            node.child = [...dataToAdd];
           } else {
            node.child = [...node.child, ...dataToAdd];
          }
         }
         })
        }
        

        然后只需调用 add 即

        add(this.data, this.childData, parentId);
        

        【讨论】:

          【解决方案5】:

          如果您想使用 parentId 合并数组,可以这样实现:

          const 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,
            }
          ];
          
          const 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
            }
          ];
          
          const result = data.map((item) => {
            const childArr = [];
            childData.forEach((childItem) => {
              if (item.id === childItem.parentId) {
                childArr.push(childItem);
              }
            });
            
            if (childArr.length > 0) {
              item.child = childArr
            }
            return item;
          });
          
          console.log(result);

          【讨论】:

            【解决方案6】:
            interface my_data {
              [key: string]: any;
            }
            
            data: my_data {
              //your data here
            }
            
            childData {
              //your data here
            }
            this.data[index]['child'] = this.childData
            

            【讨论】:

            • 请在您的回答中添加描述。所以,任何人都可以理解它:)
            猜你喜欢
            • 2019-03-24
            • 2018-10-10
            • 2016-12-25
            • 2023-03-28
            • 2020-02-08
            • 1970-01-01
            • 1970-01-01
            • 2017-05-01
            • 1970-01-01
            相关资源
            最近更新 更多