0

This is a follow up of Pushing an object into array where I was pushing an object into the array by identifying the parentActivityId. Now I wanted to remove the object based on its id.I have tried the below code based on the follow up question but its not working.Can anyone tell me what I'm doing wrong here?

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.splice(child,1);
Community
  • 1
  • 1
forgottofly
  • 2,729
  • 11
  • 51
  • 93
  • Asking a 'what is the right approach' question is almost asking to get your question down voted. :) Whatever works for you is probably the right approach. Ask yourself if the code works as expected without side effects or bugs. Do you understand what its doing, do other people need to understand what its doing. If you are OK with the answers then its the right approach for you.. – Code Uniquely Aug 21 '15 at 04:50

2 Answers2

1

This solution features Array.prototype.some() in a recursive fashion with some basic error handling.

The data is taken from Not able to push an object into parent array by identifying the parent id of the object in javascript.

The key feature is the callback for finding the needed node and the index.

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: [] }] }] }],
    id = 3,
    node;

function findNode(a, i, o) {
    if (a.id === id) {
        node = { array: o, index: i };
        return true;
    }
    return Array.isArray(a.items) && a.items.some(findNode);
}

data.some(findNode);
if (node && Array.isArray(node.array)) {
    node.array.splice(node.index, 1);
}
document.write('<pre>' + JSON.stringify(data, 0, 4) + '</pre>');
Community
  • 1
  • 1
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
  • If I want to delete node based on parentActivityId and id what change must I do to the code? – forgottofly Sep 04 '15 at 14:19
  • 1
    i assume, you would like to delete all nodes with a given `parentActivityId`, right? so in this case with a given `parentActivityId = 2`, then the node with the `id = 4` is beeing deleted. take `id = 2`, run `data.some(findNode);` and apply `node.array[node.index].items = [];`. – Nina Scholz Sep 06 '15 at 13:19
  • I'll be passing both id and parentActivityId.Based on this,the node should be deleted – forgottofly Sep 07 '15 at 06:30
  • but when you have the `id`, you get the distinct node. then you can select the `parentActivityId`, you want, by iterating over the `items` property. – Nina Scholz Sep 07 '15 at 06:42
  • my last answer is misleading. you can with the given `parentActivityId` select the node you want and with the id in the `nodes` property then select a specific child. – Nina Scholz Sep 07 '15 at 07:35
  • Can you please try this one..http://stackoverflow.com/questions/32500354/advice-on-creating-a-matrix-table-in-angular-js – forgottofly Sep 10 '15 at 11:18
0

You need to find the index of the child node in the parents item array. Should be as simple as looping over the items array of the parent until you hit the child id.

Once you have the index of the child node use that as the first parameter in the splice function

See below for a rough example (you will need to add error checking code etc for situations where the parent or child is not found)

function getParent(r, a) {
    return a.id === child.parentActivityId ? a : a.items.reduce(getParent, r);
}

var node = data.reduce(getParent, {});

var theChildIndex = 0;

for (i = 0; i < node.items.length; i++) { 
   if (node.items[i].id == child.id)
   {
       theChildIndex = i;
       break;
   }
}

node.items.splice(theChildIndex,1);
Nogusta
  • 909
  • 5
  • 10