0

This is my main data: (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,
                },

I have one child data: 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        
            }

I want to push this childData in main this.data at specific index for eg index 0,with key "child"

means my data should look like this:

{
                    "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,
                },
}

I tried this this.data[index].push(this.childData);

but didnt worked for me. Is there any method so that i can achieve this?

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);
      })
    }
6
  • Do you have both the data in different array and you want to merge them based on ID? Correct me if I am wrong Commented Oct 27, 2020 at 5:34
  • yes. i have added the method Commented Oct 27, 2020 at 5:35
  • you can create an interface and then simply do the following: this.data['child']=this.childData Commented Oct 27, 2020 at 5:36
  • @at-in i have to add it at specific index which i am getting Commented Oct 27, 2020 at 5:37
  • @ashish Check out my answer. Commented Oct 27, 2020 at 5:46

6 Answers 6

4
this.data[index]['child'] = this.childData;
   OR
this.data[index].child = this.childData;
Sign up to request clarification or add additional context in comments.

Comments

0

Create a "child" array key and push child data to it

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

Comments

0

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

Loop throgh parent records and match the ID with child records. If it finds the records then add it into you main records as a Child property and store the data.

Comments

0
// 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];
  }
 }
 })
}

Then just call add i.e

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

Comments

0

You can achieve that like this if you want to merge the arrays using 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);

Comments

0
interface my_data {
  [key: string]: any;
}

data: my_data {
  //your data here
}

childData {
  //your data here
}
this.data[index]['child'] = this.childData

1 Comment

Please add a description to your answer. So, anyone can understand it :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.