unknown
unknown

Reputation: 55

How to push object array into array with key

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

Upvotes: 0

Views: 122

Answers (6)

atin
atin

Reputation: 903

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

data: my_data {
  //your data here
}

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

Upvotes: 0

firatozcevahir
firatozcevahir

Reputation: 882

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

Upvotes: 0

Kevin Koech
Kevin Koech

Reputation: 78

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

Upvotes: 0

Vimal Patel
Vimal Patel

Reputation: 3085

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.

Upvotes: 0

cabrillosa
cabrillosa

Reputation: 155

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

Upvotes: 0

Waleed Khalid
Waleed Khalid

Reputation: 86

this.data[index]['child'] = this.childData;
   OR
this.data[index].child = this.childData;

Upvotes: 4

Related Questions