2

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);
2
  • 1
    possible duplicate of Dynamically access object propertys (JS) Commented May 19, 2015 at 6:46
  • 1
    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? Commented May 19, 2015 at 6:46

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

4 Comments

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] } } }
@user3374541 how are you checking the value?
using console.log(resultJSON);
try checking using console.log(resultJSON.person.name.deepak);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.