I have some data that
var currentData = [ {'ticket':'CAP', 'child':'CT-1'}, {'ticket':'CAP', 'child':'CT-2'}, {'ticket':'CT-1', 'child':'CT-1-A'}, {'ticket':'CT-1', 'child':'CT-1-B'} ];
The data is flat and I need to convert it to something like:
{ 'ticket': 'CAP', children : [{ 'ticket' : 'CT-1', 'children' : [{ 'ticket' : 'CT-1-A', 'children' : [] }, { 'ticket' : 'CT-1-B', 'children' : [] }], [{ 'ticket' : 'CT-2', 'children' : [] }] }] }
(I think this is true)?
I am very lost, like. I'm going to show my efforts, but I'm not sure if my approach is right or not.
var currentData = [{'ticket':'cap', 'child':'CT-1'},{'ticket':'cap', 'child':'CT-2'}, {'ticket':'CT-1', 'child':'CT-1-A'},{'ticket':'CT-1', 'child':'CT-1-B'}]; var newList = []; function convert(list){ if (newList.length <= 0){ var child = []; var emptyChild = []; child.push({'ticket': list[0].child, 'child': emptyChild }); newList.push({'ticket': list[0].ticket, 'children' : child}); list.splice(0,1); }
I think I ruined everything. In the comments, I put ** an explanation that it does not work due to the fact that JavaScript is not passed by reference, however on further reading I do not think that it is correct when I pass an object which is by reference?
Edit
Data shown with currentData will not always start at the root, or
source share