重构/重建json数据树

时间:2013-04-21 16:36:43

标签: javascript json

{
  "company": [
    { "region": [ "Europe", "Germany" ], "productLine": "Produce" },
    { "region": [ "Europe", "France" ], "productLine": "Produce" }
    ],
    "company2": [
    { "region": [ "Europe", "Germany" ], "productLine": "Produce" },
    {  "region": [ "Americas", "USA" ], "productLine": "Produce" }
    ]
}

有了这个json数据,我怎样才能重建它,以便我将欧洲/美洲的价值作为德国/法国作为孩子的主要(独特)节点?公司/公司1将是法国/德国的子女。我似乎无法弄清楚如何在保持关系正确的同时构建数组。我的本质是我需要反转节点树。

预期产出:

这样的树结构:

-Europe
   -France
      -Company
      -Company2

我还需要一个树插件的特殊结构:

var source = [ { label: "Europe", items: [
   {label: "France", items: [
      {label: "SuperShop", items: [
            {label: "Produce"}
         ]}
      ]
   }]
}]

我最终需要的是一个具有值对的Object数组:label,items。项目是具有子对象的对象。

1 个答案:

答案 0 :(得分:3)

显然,我不知道为什么你需要新的格式,但它似乎过于复杂。如果您正在查看大型数据集,那么您将会对速度产生影响,因为在它的当前设置下,您将遍历新阵列的每个元素以找到您正在查找的那个元素为......

var inputs = {
  "company": [
    { "region": [ "Europe", "Germany" ], "productLine": "Produce" },
    { "region": [ "Europe", "France" ], "productLine": "Produce" }
    ],
    "company2": [
    { "region": [ "Europe", "Germany" ], "productLine": "Produce" },
    {  "region": [ "Americas", "USA" ], "productLine": "Produce" }
    ]
};

var converter = {};

// This new format requires a 2 step process to prevent it from being N^2
// So convert the input into a tree
//   Region
//     -> Country
//       -> Company
//         -> Array of Products
for(var company in inputs){
  for(var i = 0; i < inputs[company].length; i++){
    // Because the regions are an array of hashes it is simplest
    // to grab the value by using the previously gathered keys
    // and the key region
    var r = inputs[company][i]['region'];

    // Check if the region exists.  If not create it.
    if(!converter[r[0]]){
      converter[r[0]] = {};
    }
    // Check if the country exists.  If not create it.
    if(!converter[r[0]][r[1]]){
      converter[r[0]][r[1]] = {};
    }
    // Add the company to the array.
    if(!converter[r[0]][r[1]][company]){
      converter[r[0]][r[1]][company] = [];
    }
    converter[r[0]][r[1]][company].push(inputs[company][i]['productLine']);
  }
}

var outputs = [];

// Now walk converter and generate the desired object.
for( var region in converter){
  converted_region = {};
  converted_region["label"] = region;
  converted_region["items"] = [];
  for( var country in converter[region]){
    converted_country = {};
    converted_country["label"] = country;
    converted_country["items"] = [];
    for( var company in converter[region][country]){
      converted_company = {};
      converted_company["label"] = company;
      converted_company["items"] = [];
      for(var i = 0; i < converter[region][country][company].length; i++){
        converted_company["items"].push(converter[region][country][company][i]);
      }
      converted_country["items"].push(converted_company);
    }
    converted_region["items"].push(converted_country);
  }
  outputs.push(converted_region);
}