最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to create a complexnested js object dynamically? - Stack Overflow

programmeradmin0浏览0评论

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.

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.

Share Improve this question edited Jul 14, 2015 at 13:33 cafebabe1991 asked Jul 13, 2015 at 18:36 cafebabe1991cafebabe1991 5,1862 gold badges37 silver badges43 bronze badges 6
  • Did you try JSON.parse(text); ? – SD. 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 – cafebabe1991 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'? – Rick Hitchcock Commented Jul 13, 2015 at 18:52
  • Try return createHierarchy(...) – thefourtheye 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. – Vale Commented Jul 13, 2015 at 18:53
 |  Show 1 more ment

3 Answers 3

Reset to default 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);
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": {}
      }
    }
 }
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/qo21eh84/2/

发布评论

评论列表(0)

  1. 暂无评论