Update nested attributes in a JavaScript object - Javascript Array Operation

Javascript examples for Array Operation:Array Nest

Description

Update nested attributes in a JavaScript object

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <script type="text/javascript">
    window.onload=( function() {//from w w  w .  j  ava2s .c o  m
var data = {level1: {level2 : 'test' }};

var parents = 'level1/level2'.split('/');

var target = data;

for(var i=0; i<parents.length-1; i++){
   target = target[parents[i]];
}
target[parents[i]] = 'new value';

console.log( data.level1.level2 );
    });

      </script> 
   </head> 
   <body>  
   </body>
</html>

Related Tutorials