1

I am trying to set one of the nested subobject properties, but the nested level is dynamic.

how can I dynamically set the nested properties?

It's working only one level properties,i can't set next inner level....

my code:

function deSerialize(qualifiedNameArray, currentIndex, resultJSON, valueToBeInitializedForFinalNode)
{
    if (currentIndex  == (qualifiedNameArray.length - 1)){
        resultJSON [qualifiedNameArray[currentIndex++]] = valueToBeInitializedForFinalNode;
    }
    else
    {
        resultJSON [qualifiedNameArray[currentIndex++]] = {};
    }

    if (currentIndex < qualifiedNameArray.length)
        deSerialize( qualifiedNameArray, currentIndex, resultJSON, valueToBeInitializedForFinalNode);

    return resultJSON;
}


  var results = {"columnname":"person.name.first", "varcharvalue":"david", "objecttype" : "user"};
    var valueToBeInitializedForFinalNode = results["varcharvalue"];
    var qualifiedNameArray = results["columnname"].split('.');
    var resultJSON = {};
    deSerialize(qualifiedNameArray, 0, resultJSON, valueToBeInitializedForFinalNode);
Roli Agrawal
  • 2,356
  • 3
  • 23
  • 28
  • possible duplicate of [Dynamically access object propertys (JS)](http://stackoverflow.com/questions/25306541/dynamically-access-object-propertys-js) – Grundy May 19 '15 at 06:46
  • So, in few words, if you've got a path, you'd like to set that path in an object to that value? I'm not sure what you want to do with currentIndex. Are you trying to serialize an array of objects? – Jack May 19 '15 at 06:46

1 Answers1

1

A simple solution might be, not sure if this is what you are looking for:

function makeObj(arry, initValue){
    var obj = {}, objRef  = obj, idx = 0;    
    while(idx < arry.length -1){
        obj[arry[idx]] = {};
        obj = obj[arry[idx]];
        idx++;
    }
    obj[arry[idx]] = initValue;
    return objRef;
}

usage:

resultJSON = makeObj( qualifiedNameArray, valueToBeInitializedForFinalNode);

another way is:

function makeObj(objRef, arry, initValue){
    var obj = objRef, idx = 0;    
    while(idx < arry.length -1){
        if(!obj[arry[idx]]) obj[arry[idx]] = {};
        obj = obj[arry[idx]];
        idx++;
    }
    if(!obj[arry[idx]]) obj[arry[idx]] = initValue;
}

this way, you do not change any values that might have been already present, usage:

makeObj( resultJSON, qualifiedNameArray, valueToBeInitializedForFinalNode);
mido
  • 24,198
  • 15
  • 92
  • 117
  • Hi, It's working only 3 level object...if i add one more inner object not working..........example..........var results = {"columnname":"person.name.deepak.samuel", "varcharvalue":"david", "objecttype" : "user"}; results is : { person: { name: { deepak: [Object] } } } – deepak samuel May 19 '15 at 06:59
  • @user3374541 how are you checking the value? – mido May 19 '15 at 07:01
  • using console.log(resultJSON); – deepak samuel May 19 '15 at 07:15
  • try checking using `console.log(resultJSON.person.name.deepak); ` – mido May 19 '15 at 07:17