2013-04-21 39 views
0
{ 
    "company": [ 
    { "region": [ "Europe", "Germany" ], "productLine": "Produce" }, 
    { "region": [ "Europe", "France" ], "productLine": "Produce" } 
    ], 
    "company2": [ 
    { "region": [ "Europe", "Germany" ], "productLine": "Produce" }, 
    { "region": [ "Americas", "USA" ], "productLine": "Produce" } 
    ] 
} 

有了这个json数据我该如何重建它,以便将欧洲/美洲的价值作为德国/法国的主要(独特)节点,因为它是儿童?公司/公司1将是法国/德国的子子公司。我似乎无法弄清楚如何在保持关系正确的情况下构建数组。我的本质我需要扭转节点树。重构/重建json数据树

预期输出:

树结构是这样的:

-Europe 
    -France 
     -Company 
     -Company2 

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

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

我需要的到底是一个对象具有值对的数组:标签,项目。项目是内部具有子对象的对象。

+0

添加给定数据的预期输出。 – shakib 2013-04-21 17:29:57

+0

期望的输出是否需要是json对象,或者它可以是数组/散列? – miah 2013-04-21 17:53:04

+0

只要结构良好,它可以是数组。 – DominicM 2013-04-21 17:55:53

回答

3

显然,我不知道为什么你需要新的格式,但它似乎过于复杂。如果你有一个大的数据集,你正在浏览速度,因为在它目前的设置下,你将会遍历新数组的每一个元素来找到你正在寻找的那个数据集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); 
} 
+0

“['region']”是什么意思?因为它们是动态的,所以我不会有区域值。 – DominicM 2013-04-21 18:50:06

+0

不要忘记'var'!并且不要在数组中使用for-in-loop! – Bergi 2013-04-21 18:51:24

+0

@DominicM给出你描述的输入,以达到[地区,国家]的价值,你必须使用输入[公司] [index_of_array] [“地区”] – miah 2013-04-21 18:57:00