1

I have the following data.

var data = "a.b.c.d"; //Just an example but can be more deep.

  1. A nested structure as a string to create a.b.c.n

Now i want to create a js object from this data like this..

{
  "a":{
     "b":{
        "c":{
            and so on till the given depth.  
          }
       }
  }

}

What i have tried

function createHierarchy( obj, group, i){

    if(i === group.length){
        return obj;
    }
    else{
        if(obj[group[i]] === undefined)
        {
            obj[group[i]] = new Object();

        }

        createHierarchy(obj[group[i]], group, ++i); 
    }
}

Problem

This function is returning me undefined as i am sending the newly created subobject in every recursive call and since the newly created object is {} , hence the final result is undefined.

Update

I want to create the object only if it does not exist.For eg : if d already exists ill insert a value into it. else ill create it.

So this is what i added to @Tholle's answer.

if(temp[array[i]] === undefined)
            temp = temp[array[i]] = {};
        else
            temp = temp[array[i]][name] = value;

So kindly suggest a way out.

6
  • Did you try JSON.parse(text); ? Commented Jul 13, 2015 at 18:45
  • @StoyanDekov I dont think that would help, as the text that parse function needs has to be a an js object(of type string) ...Right ? And i don't have that Commented Jul 13, 2015 at 18:47
  • Can you provide an example of your nested structure, or is it simply a string such as 'a.b.c.n'? Commented Jul 13, 2015 at 18:52
  • Try return createHierarchy(...) Commented Jul 13, 2015 at 18:52
  • How is the data you're getting structured? If you provide an example we might be able to give better suggestions. Commented Jul 13, 2015 at 18:53

3 Answers 3

7
var data = "a.b.c.n";
var array = data.split(".");

var result = {};
var temp = result;
for(var i = 0; i < array.length; i++) {
    temp = temp[array[i]] = {};
}

console.log(result);
Sign up to request clarification or add additional context in comments.

5 Comments

temp and result point to same location and hence every change in temp is reflected in result ? Is that it ?
Right on the money. And for every iteration in the loop, the temp variable references one level deeper, and the result variable is just a reference to the entire object.
One more thing , i would like to create an object only if it does not exist.
@cafebabe1991 Consider making another question, since this one is answered.
I think its just an enhancement so i did not add a new question....But i did it , ill update the post. Thanks .
2
function BuildObj(string) {
  var obj = {};
  var params = string.split(".");
  for(var i=params.length-1;i>=0;i--) {
    last = obj;
    obj = {};
    obj[params[i]] = last;
  }
  return obj;
}

So,

buildObj("a.b.c.d");

Gives:

{
  "a": {
    "b": {
      "c": {
        "d": {}
      }
    }
 }

Comments

1
var data = "a.b.c.n";
var array = data.split(".");

var last = {};
var temp = {};
for (var i = array.length - 1; i >= 0; i--) {
  temp = {};
  temp[array[i]] = last;
  last = temp;
}

console.log(last);

https://jsfiddle.net/qo21eh84/2/

Comments

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.