如何连接两个对象

时间:2015-11-16 17:48:29

标签: javascript

我想知道如何使用JavaScript将parents.json与childrens.json连接起来。我需要得到result.json

第一个对象:parents.json

{
    "name": "ParentsLevel2",
    "Level": 1,
    "children": [
        {
            "name": "analytics",
            "level": 2,
        },
        {
            "name": "animate",
            "level": 2
        }
    ]

}

第二个对象:childrens.json

{
    "name": "childrensLevel3",
    "Level": 1,
    "children": [
        {
            "name": "analytics1",
            "level": 3,
            "parent": "analytics"
        },
        {
         "name": "analytics2",
         "level": 3,
         "parent": "analytics"
         },
        {
         "name": "animate1",
         "level": 3,
         "parent": "animate"
         },
          {
         "name": "animate2",
         "level": 3,
         "parent": "animate"
         }
    ]

结果:result.json

{
    "name": "Root",
    "Level": 1,
    "children": [
        {
            "name": "analytics",
            "level": 2,
            "children": [
                {
                   "name": "analytics1",
                    "level": 3
                },
                {
                 "name": "analytics2",
                  "level": 3
                }

            ]
        },
        {
            "name": "animate",
            "level": 2
             "children": [
                {
                   "name": "animate1",
                    "level": 3
                }

            ]
        }
    ]

}

3 个答案:

答案 0 :(得分:0)

试试这段代码

var obj1 = { "name": "ParentsLevel2", "Level": 1, "children": [ { "name": "analytics", "level": 2, }, { "name": "animate", "level": 2 } ]

};
var obj2 = { "name": "childrensLevel3", "Level": 1, "children": [ { "name": "analytics1", "level": 3, "parent": "analytics" }, { "name": "analytics2", "level": 3, "parent": "analytics" }, { "name": "animate1", "level": 3, "parent": "animate" }, { "name": "animate2", "level": 3, "parent": "animate" } ]};
var obj = {};
obj [0] = obj 1;
obj [1] = obj 2;
console.log(obj);

答案 1 :(得分:0)

我有这个:

      var parent= parents.json
      var child=  childrens.json
      var obj=parent["children"];
      var obj2=child["children"];

      for(i in obj){
      for(c in obj2){
      if(obj[i].name == obj2[c].parent){
     //here is the push of the child associated to the parent,  but how and
     //also how create the result.json I need to create other object to put all together.
      } 
      }
      }

答案 2 :(得分:0)

解决方案:

 var parentJson={
 "name": "ParentsLevel2",
 "Level": 1,
 "children": [
    {
        "name": "analytics",
        "level": 2,
         "children":[]
    },
    {
        "name": "animate",
        "level": 2,
        "children":[]
    }
]

};

var sonJson={
"name": "ChildrenLevel3",
"Level": 1,
"children": [
    {
        "name": "analytics1",
        "level": 3,
        "parent": "analytics"

    },
     {
        "name": "analytics2",
        "level": 3,
        "parent": "analytics"

    }
]

  };


  //push a new object to the parentJson
 //parentJson["children"].push("object to push");

   //push a new object to the sonJson
  //sonJson["children"].push("object to push");


//join 
  var objSon=sonJson["children"];
  var oP=parentJson["children"];

  for(p in oP){
  for(s in objSon){

  if(oP[p].name==objSon[s].parent){
     alert("match");
   oP[p].children.push(objSon[s]);
  }
 }
}



//create the result
var result=[];
var obj=oP;
for(i in obj){
var temp=[];
temp.push("FirstName");
temp.push(obj[i].name);
temp.push("Level");
temp.push(obj[i].level);
temp.push("Hijos")
temp.push(obj[i].children);
result.push(temp);

 $("#D3").html(JSON.stringify(result));



  $("#D3").prepend("result=")

jsfiddle Solution