Recursively Search and Update a JSON object
<p>JSON question for you guys.</p>

<p>Say I have an JSON object like so, with each node being an object with an ID and an array of further nodes:</p>

<pre><code>var tree = {
    id: 0,
    items: [{
            id: 48,
            items: [{id: 67, items :[]}]
        }, {
            id: 126,
            items: []
        }]
}
</code></pre>

<p>Say I wanted to update the object with a new array of items at the appropriate node. So for example, the following would occur using the above tree object:</p>

<pre><code>function updateTree(id, array) {
    //code goes here
}
var data = [{id: 121, items :[]}]

tree = UpdateTree(67, data)
</code></pre>

<p>and tree would come out to equal:</p>

<pre><code>tree = {
    id: 0,
    items: [{
            id: 48,
            items: [{id: 67, items : [{id: 121, items :[]}]}]
        }, {
            id: 126,
            items: []
        }]
}
</code></pre>

<p>how would I implement updateTree here? the recursion is ripping my brain apart. Thanks! Here's a Fiddle: <a href="https://jsfiddle.net/nus2jyvg/" rel="nofollow">https://jsfiddle.net/nus2jyvg/</a></p>