2013-04-21 3 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 데이터를 사용하여 어떻게하면 유럽/아메리카의 값이 독일/프랑스의 기본 (고유 한) 노드로 어린이에게 재 구축 될 수 있습니까? company/company1은 프랑스/독일의 하위 하위 그룹입니다. 관계를 올바르게 유지하면서 배열을 만드는 방법을 알아낼 수 없습니다. 나는 노드 트리를 뒤집을 필요가있다.json 데이터 트리 재구성/재구성

예상 출력 :이 같은

트리 구조 :

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

은 내가 결국 필요한 것은 객체이다 : 나는 또한 나무 플러그인위한 특별한 구조가 필요

-Europe 
    -France 
     -Company 
     -Company2 

값 쌍이있는 배열 : label, items. 항목은 하위 오브젝트가있는 오브젝트입니다.

+0

주어진 데이터에 대해 의도 한 결과를 추가하십시오. – shakib

+0

예상되는 출력이 json 객체 일 필요가 있거나 배열/해시 일 수 있습니까? – miah

+0

잘 구조화되어있는 한 배열 일 수 있습니다. – DominicM

답변

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

"[ 'region']"은 무엇을 의미합니까? 역동적이므로 지역 값을 제시하지 않겠습니다. – DominicM

+0

'var'을 잊지 마세요! 배열을 사용하여 for-in-loops를 사용하지 마십시오! – Bergi

+0

@DominicM 당신이 입력 한 [지역, 국가] 값을 얻으려면 입력 [company] [index_of_array] [ "region"] – miah