Failed to push object to parent array specifying parent id of object in javascript

I have a JSON array with properties like id and parentActivityId.

$scope.data = [ { id: 1, activityName: "Drilling", parentActivityId: 0, items: [ { id: 2, activityName: "Blasting", parentActivityId: 1, items: [ { id: 3, activityName: "Ann", parentActivityId: 2, items: [], }, { id: 4, activityName: "Ann", parentActivityId: 2, items: [], } ] }, { id: 5, activityName: "Transport", parentActivityId: 1, items: [ { id: 6, activityName: "Daniel", parentActivityId: 5, items: [], } ] } ] } ]; 

I would like to push a new element based on parentActiityId . The new item will have a new id . Example. If my object looks like this:

 { id: 7, activityName: "Drilling", parentActivityId: 1, items: [] } 

then my object will look like this.

  $scope.data = [ { id: 1, activityName: "Drilling", parentActivityId: 0, items: [ { id: 2, activityName: "Blasting", parentActivityId: 1, items: [ { id: 3, activityName: "Ann", parentActivityId: 2, items: [], }, { id: 4, activityName: "Ann", parentActivityId: 2, items: [], }, { id: 7, activityName: "Drilling", parentActivityId: 2, items: [], } ] }, { id: 5, activityName: "Transport", parentActivityId: 1, items: [ { id: 6, activityName: "Daniel", parentActivityId: 5, items: [], } ] } ] } ]; 

I tried to give this for a loop.

 var arrObj = { id: 7, activityName: "Drilling", parentActivityId: 1, items: [] }; function populateObj(arrObj) { for (var i = 0; i < $scope.data.length; i++) { if ($scope.data[i].id == arrObj.parentActivityId) { $scope.data.push(arrObj); } } }; populateObj(arrObj); 

which will only point to the parent. I also want to identify the child in a for loop and click on a specific array object by specifying thr parentActivityId. Any help would be really appreciated.

+8
javascript arrays angularjs push for-loop
source share
3 answers

With recursive clause and Array.prototype.reduce() :

 var data=[{id:1,activityName:"Drilling",parentActivityId:0,items:[{id:2,activityName:"Blasting",parentActivityId:1,items:[{id:3,activityName:"Ann",parentActivityId:2,items:[]},{id:4,activityName:"Ann",parentActivityId:2,items:[]}]},{id:5,activityName:"Transport",parentActivityId:1,items:[{id:6,activityName:"Daniel",parentActivityId:5,items:[]}]}]}], child = { id: 7, activityName: "Drilling", parentActivityId: 1, items: [] }; function getParent(r, a) { return a.id === child.parentActivityId ? a : a.items.reduce(getParent, r); } var node = data.reduce(getParent, {}); 'items' in node && node.items.push(child); document.write('<pre>' + JSON.stringify(data, 0, 4) + '</pre>'); 
+5
source share

You were close to what you were trying to achieve. This should do the trick:

 var arrObj = { id: 7, activityName: "Drilling", parentActivityId: 1, items: [] }; function populateObj(data, arrObj) { for (var i = 0; i < data.length; i++) { if (data[i].id == arrObj.parentActivityId) { data[i].items.push(arrObj); } else { populateObj(data[i].items, arrObj); } } }; populateObj(arrObj); 
+2
source share

To check the objects inside an array of elements, you can do something like this,

 var objectList = [ { id: 1, activityName: "Drilling", parentActivityId: 0, items: [ { id: 2, activityName: "Blasting", parentActivityId: 1, items: [ { id: 3, activityName: "Ann", parentActivityId: 2, items: [], }, { id: 4, activityName: "Ann", parentActivityId: 2, items: [], }, { id: 7, activityName: "Drilling", parentActivityId: 2, items: [], } ] }, { id: 5, activityName: "Transport", parentActivityId: 1, items: [ { id: 6, activityName: "Daniel", parentActivityId: 5, items: [], } ] } ] } ]; var arrObj = { id: 7, activityName: "Drilling", parentActivityId: 1, items: [] }; function populateObj(ItemList) { for (var i = 0; i < ItemList.length; i++) { if (ItemList[i].id == arrObj.parentActivityId) { ItemList[i].items.push(arrObj); } else { populateObj(ItemList[i].items); } } }; populateObj(objectList); 
0
source share

All Articles