将对象属性放在另一个对象中

时间:2016-03-14 20:31:05

标签: javascript arrays angularjs object

这可能很简单,但我似乎无法找到如何在Javascript中合并2个对象

例如:对象一

[
  {
    "name":"Actief",
    "children":[
      {
        "name": "Vaste activa",
        "children": [
          {
            "name": "Immateriële vaste activa",
            "children": null
          },
          {
            "name": "Materiële vaste activa",
            "children": [
              {
                "name": "Terreinen en gebrouwen",
                "children": null
              },
              {
                "name": "Installaties, machines & uitrustingen",
                "children": null
              },
            ]
          }
        ]
      }
    ]
  }
]

对象二:

[
   {
      name: "Immateriële vaste activa",
      data: [1.3, 2.4, 3.6, 0.2, 1.1],
      year: [2009, 2010, 2011, 2012, 2013]
   },
   {
      name: "Terreinen en gebrouwen",
      data: [1000300, 44589, 908887, 255667, 110000],
      year: [2009, 2010, 2011, 2012, 2013]
   },
   {
      name: "Installaties, machines & uitrustingen",
      data: [12000, 23000, 38000, 203000, 11111],
      year: [2009, 2010, 2011, 2012, 2013]
   }
]

所以为了更多地解释一下,我正在构建一个AngularJS应用程序,在这个例子中,我在ng-repeat内做ng-repeat所以我做了某种树的第一个JSON对象。最低级别(有时5深)必须包含数据和年份,所以我试图找出如何合并这些多维数组/对象。

对我来说,最好的是拥有这样的东西:

{
   "name": "Terreinen en gebrouwen",
   "children": null,
   "data": [1000300, 44589, 908887, 255667, 110000],
   "year": [2009, 2010, 2011, 2012, 2013]
},

在我的视图中发布此内容仍然很难,但我认为如果我的对象/数组看起来像这样,我将会更进一步。

1 个答案:

答案 0 :(得分:1)

这看起来很简单。

您只需要遍历 Object two 的项目,然后在 Object one 中找到具有相同名称的对象

我们的两个对象:

var obj1 = [
  {
    "name":"Actief",
    "children":[
      {
        "name": "Vaste activa",
        "children": [
          {
            "name": "Immateriële vaste activa",
            "children": null
          },
          {
            "name": "Materiële vaste activa",
            "children": [
              {
                "name": "Terreinen en gebrouwen",
                "children": null
              },
              {
                "name": "Installaties, machines & uitrustingen",
                "children": null
              },
            ]
          }
        ]
      }
    ]
  }
]

var obj2 = [
   {
      name: "Immateriële vaste activa",
      data: [1.3, 2.4, 3.6, 0.2, 1.1],
      year: [2009, 2010, 2011, 2012, 2013]
   },
   {
      name: "Terreinen en gebrouwen",
      data: [1000300, 44589, 908887, 255667, 110000],
      year: [2009, 2010, 2011, 2012, 2013]
   },
   {
      name: "Installaties, machines & uitrustingen",
      data: [12000, 23000, 38000, 203000, 11111],
      year: [2009, 2010, 2011, 2012, 2013]
   }
]

然后在名称相同的情况下添加属性的函数或者如果有的话,进入子数组并重试:

function addProps(obj, o){

        for (var j = 0; j < obj.length; j++) {
            var o2 = obj[j];
            if(o2.name === o.name) {
                o2.data = o.data; 
                o2.year = o.year; 
            } else {
                if(o2.children != null) addProps(o2.children, o);
            }
        }

}

现在在我的代码中迭代 Object two obj2

for (var i = 0; i < obj2.length; i++) {
    var o = obj2[i];
    addProps(obj1, o);
}