Javascript go to inner key by an index -


i have javascript object this:

 var json = {     : "xxxx",     b : {         p : 12,         : "xxxx",         b : {             r : 1,             : "xxxx",             b : null         }     } } 

and want function passing in parameters key, index , object, add last object object @ inner level indicated index.
know, have not explained well, example:

function addobjecttojson(key,index,object){     var helpjson = json;     for(var = 0; < index; i++){         helpjson = helpjson[key];     }     helpjson = object;       } 

the above function not work.

i want result of this:

addobjecttojson("b",2,{ a: 2, b: null}); 

was:

var json = {     : "xxxx",     b : {         p : 12,         : "xxxx",         b : {             r : 1,             : "xxxx",             b : {                  : 2,                  b : null                }         }     } } 

var json = { : "xxxx", b : {     p : 12,     : "xxxx",     b : {         r : 1,         : "xxxx",         b : null     } } 

}

//addobjecttojson

function addobjecttojson(key,index,object,indexes = ""){        if(index < 0){           return false;       }       var jsonhelp = json;      if(indexes != ""){         var indexessplit = indexes.split(".");          for(var = 0; < indexessplit.length; i++){                     jsonhelp = jsonhelp[indexessplit[i]];                     }     }else{       if(index == 0){           json[key] = object;           console.log(json);           return false;       }     }       for(keyhelp in jsonhelp){         if(key == keyhelp){             index--;             if(indexes == ""){                 indexes = key;             }else{                 indexes += "."+key;             }              if(index > -1){                 addobjecttojson(key,index,object,indexes);                 break;             }         }            };        if(index <= -1){        jsonhelp[key] = object;              var indixeshelp = ""        for(var = 0; < indexessplit.length; i++){                    indixeshelp += "."+indexessplit[i];                 }        eval("json"+indixeshelp+" = jsonhelp");                 console.log(json);    }   } 

Comments

Popular posts from this blog

'hasOwnProperty' in javascript -

python - ValueError: No axis named 1 for object type <class 'pandas.core.series.Series'> -

java - How to provide dependency injections in Eclipse RCP 3.x? -