简体   繁体   English

重组/重建json数据树

[英]Restructure / rebuild json data tree

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

With this json data how can I rebuild it so that I have Europe/Americas value as the primary(unique) node with Germany/France as it's children? 有了这个json数据,我该如何重建它,以使Europe / Americas值成为德国/ France作为子级的主要(唯一)节点? company/company1 would be sub-children of France/Germany. company / company1将是法国/德国的子子级。 I cant seem to figure out how to build arrays while keeping the relations correct. 我似乎无法弄清楚如何建立数组同时保持正确的关系。 I essence I need to reverse the node tree. 我实质上需要反转节点树。

Expected Output: 预期产量:

Tree structure like this: 像这样的树结构:

-Europe
   -France
      -Company
      -Company2

I also need a special structure for a tree plugin: 我还需要一个特殊的树插件结构:

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

What I need in the end is an Object array with value pair: label, items. 我最后需要的是一个带有值对的Object数组:标签,项目。 Items being an object with sub-objects within. 项目是一个对象,其中包含子对象。

Obviously, I don't know why you need the new format, but it seems overly complex. 显然,我不知道为什么需要新格式,但是它看起来过于复杂。 If you have a large data set that you are looking through, you are going to take a hit on speed because, under it's current set up, you are going to have traverse over every element of the new array to find the one you are looking for ... 如果您要查看的是大型数据集,那么将极大地提高速度,因为在当前设置下,您将遍历新数组的每个元素以查找所需的元素为了...

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM