title: Walk and set value to complex Javascript object
body: <p>I note the following similarity to this post:
  <a href="https://stackoverflow.com/questions/6842795/dynamic-deep-setting-for-a-javascript-object">Dynamic deep setting for a JavaScript object</a></p>
  
  <p>However, the above post is based upon a known structure and depth to the javascript object and not truly dynamic.  Truly dynamic would suggest that you did not have any precursor knowledge of the structure, just a path and a value to replace it with.  I have created a fairly good use case over on JSFiddle here:  </p>
  
  <p><a href="http://jsfiddle.net/kstubs/nJrLp/1/" rel="nofollow noreferrer">http://jsfiddle.net/kstubs/nJrLp/1/</a></p>
  
  <pre><code>function Message(message) {
  $('result').insert('&lt;div&gt;' + message + '&lt;/div&gt;');
  }
  
  var obj = {
  "array": [1, 2, 3],
  "boolean": true,
  "null": null,
  "number": 123,
  "object": {
      "a": "b",
      "c": "d",
      "e": "f",
      "complex_array1": [{
          "g": "h"
      }, {
          "bingo": "bongo"
      }, {
          "x": {
              "complex_array2": [{
                  "h": "i"
              }, {
                  "j": "k"
              }, {
                  "bingo": "bongo"
              }, {
                  "bango": "jango"
              }]
          }
      }]
  },
  "string": "Hello World"
  };
  
  var list = [{
  "h": "i"
  }, {
  "j": "k"
  }];
  
  function walk(path,value) {
  var a = path.split('.');
  var context = obj;
  
  for (i = 0; i &lt; a.size(); i++) {
      context = context[a[i]];
  }
  </code></pre>
  
  <p>}</p>
  
  <p>The use case:</p>
  
  <ol>
  <li>Find complex_array2</li>
  <li>Update its list to a new list (a new array)</li>
  </ol>
  
  <p>The new array is the array list which should replace the list for complex_array2.  The javascript function <em>walk</em> does just that, walks the javascript object until the path criteria is met and then sets the value to whatever value is passed to the walk function, however the new value does not stick.</p>
  
  <p>I know why it doesn't stick, because as you walk over an object of type array you lose the pointer to the original object.  So, the challenge is to <strong>walk</strong> the javascript object and not lose context of the original object.  </p>
  
  <p>Thanks for any assistance.</p>
  
  <p>Karl..</p>