I have a hierarchical data structure such as this:
var tree = [ {foo: 1, children:[
{foo: 2, children:[
{foo: 13, children:[]},
{foo: 14, children:[]}
]},
{foo: 3, children:[]},
{foo: 4, children:[]}
]},
{foo: 5, children:[
{foo: 6, children:[]},
{foo: 8, children:[]}
]},
{foo: 9, children:[
{foo: 10, children:[]},
{foo: 11, children:[]},
{foo: 12, children:[]}
]} ];
The tree can be of any depth.
In order to reposition a specific object in the tree (including its children), I can trivially write:
// Move object from [0, 0, 1] to [2, 1]
var obj = tree[0]['children'][0]['children'][1];
tree[0]['children'][0]['children'].splice(1, 1);
tree[2]['children'].splice(1, 0, obj);
But I am not able to program the general case:
Given two sets of coordinates, reposition an object from [i1, i2, ..., im] to [j1, j2, ..., jn].
I would like some hints about how to construct this recursive algorithm. Although this is a pure Javascript question, I should note that my application uses AngularJS and jQuery. Perhaps these libraries provide array manipulation functions that I could use?