2017-03-03 107 views
1

我有以下數據結構:遍歷層次在Javascript

{ 
    "data":[ 
     { 
     "id":1, 
     "name":"EU Group", 
     "parent_id":null, 
     "children":[ 
      { 
       "id":2, 
       "name":"EMEA Branch", 
       "parent_id":1, 
       "children":[ 
        { 
        "id":5, 
        "name":"UK", 
        "parent_id":2, 
        "lr":"," 
        }, 
        { 
        "id":6, 
        "name":"France", 
        "parent_id":2, 
        "lr":"," 
        }, 
        { 
        "id":7, 
        "name":"Germany", 
        "parent_id":2, 
        "lr":"," 
        }, 
        { 
        "id":8, 
        "name":"Spain", 
        "parent_id":2, 
        "lr":"," 
        }, 
        { 
        "id":9, 
        "name":"Finland", 
        "parent_id":2, 
        "lr":"," 
        }, 
        { 
        "id":10, 
        "name":"Hungary", 
        "parent_id":2, 
        "lr":"," 
        }, 
        { 
        "id":11, 
        "name":"Poland", 
        "parent_id":2, 
        "children":[ 
         { 
          "id":25, 
          "name":"Romania", 
          "parent_id":11, 
          "lr":"," 
         } 
        ], 
        "lr":"," 
        }, 
        { 
        "id":12, 
        "name":"Sweden", 
        "parent_id":2, 
        "lr":"," 
        }, 
        { 
        "id":16, 
        "name":"Italy", 
        "parent_id":2, 
        "lr":"," 
        }, 
        { 
        "id":17, 
        "name":"Czech", 
        "parent_id":2, 
        "lr":"," 
        }, 
        { 
        "id":18, 
        "name":"Denmark", 
        "parent_id":2, 
        "lr":"," 
        }, 
        { 
        "id":19, 
        "name":"Rest of the world", 
        "parent_id":2, 
        "lr":"," 
        }, 
        { 
        "id":36, 
        "name":"Middle East", 
        "parent_id":2, 
        "children":[ 
         { 
          "id":21, 
          "name":"Russia", 
          "parent_id":36, 
          "lr":"," 
         } 
        ], 
        "lr":"," 
        }, 
        { 
        "id":38, 
        "name":"Africa", 
        "parent_id":2, 
        "children":[ 
         { 
          "id":49, 
          "name":"Algeria", 
          "parent_id":38, 
          "lr":"," 
         }, 
         { 
          "id":50, 
          "name":"Morocco", 
          "parent_id":38, 
          "lr":"," 
         } 
        ], 
        "lr":"," 
        }, 
        { 
        "id":48, 
        "name":"Belgium", 
        "parent_id":2, 
        "lr":"," 
        } 
       ], 
       "lr":"," 
      }, 
      { 
       "id":45, 
       "name":"International Branch", 
       "parent_id":1, 
       "lr":"," 
      }, 
      { 
       "id":46, 
       "name":"APAC Branch", 
       "parent_id":1, 
       "children":[ 
        { 
        "id":51, 
        "name":"Singapore", 
        "parent_id":46, 
        "lr":"," 
        }, 
        { 
        "id":52, 
        "name":"China", 
        "parent_id":46, 
        "lr":"," 
        }, 
        { 
        "id":53, 
        "name":"Vietnam", 
        "parent_id":46, 
        "lr":"," 
        }, 
        { 
        "id":54, 
        "name":"Indonesia", 
        "parent_id":46, 
        "lr":"," 
        }, 
        { 
        "id":55, 
        "name":"India", 
        "parent_id":46, 
        "lr":"," 
        }, 
        { 
        "id":56, 
        "name":"Malaysia", 
        "parent_id":46, 
        "lr":"," 
        } 
       ], 
       "lr":"," 
      } 
     ], 
     "lr":"," 
     } 
    ] 
} 

我想要使用此功能,我創建了它遍歷:

buildHierarchy(item) { 
    zone.children.forEach((child) => { 
     this.buildHierarchy(child); 
    }) 
} 

這是在所謂的進一步下跌我的渲染功能:

render() { 
    ... 
    if (zones[0]) { 
     this.buildHierarchy(zones); 
     ... 
    } 
    ... 
} 

但是,當它碰到一個沒有孩子的物品時會中斷。我怎樣才能使這個工作?

+1

只需添加,如果要檢查是否有孩子嗎? – Leon

+0

你應該檢查zone.children是否爲空? –

+0

@Leon,但它會跳出循環,並可能錯過其他可能有或沒有孩子的物品。 – imperium2335

回答

0

放在兒童支票使用上forEach前:

buildHierarchy(zone) { 
    if(zone && zone.children) 
     zone.children.forEach(child => { this.buildHierarchy(child) }) 
} 

我覺得這是一個不匹配,你需要接受zone,而不是itembuildHierarchy方法。

檢查這個代碼:

let data = { 
 
    "data":[ 
 
     { 
 
     "id":1, 
 
     "name":"EU Group", 
 
     "parent_id":null, 
 
     "children":[ 
 
      { 
 
       "id":2, 
 
       "name":"EMEA Branch", 
 
       "parent_id":1, 
 
       "children":[ 
 
        { 
 
        "id":5, 
 
        "name":"UK", 
 
        "parent_id":2, 
 
        "lr":"," 
 
        }, 
 
        { 
 
        "id":6, 
 
        "name":"France", 
 
        "parent_id":2, 
 
        "lr":"," 
 
        }, 
 
        { 
 
        "id":7, 
 
        "name":"Germany", 
 
        "parent_id":2, 
 
        "lr":"," 
 
        }, 
 
        { 
 
        "id":8, 
 
        "name":"Spain", 
 
        "parent_id":2, 
 
        "lr":"," 
 
        }, 
 
        { 
 
        "id":9, 
 
        "name":"Finland", 
 
        "parent_id":2, 
 
        "lr":"," 
 
        }, 
 
        { 
 
        "id":10, 
 
        "name":"Hungary", 
 
        "parent_id":2, 
 
        "lr":"," 
 
        }, 
 
        { 
 
        "id":11, 
 
        "name":"Poland", 
 
        "parent_id":2, 
 
        "children":[ 
 
         { 
 
          "id":25, 
 
          "name":"Romania", 
 
          "parent_id":11, 
 
          "lr":"," 
 
         } 
 
        ], 
 
        "lr":"," 
 
        }, 
 
        { 
 
        "id":12, 
 
        "name":"Sweden", 
 
        "parent_id":2, 
 
        "lr":"," 
 
        }, 
 
        { 
 
        "id":16, 
 
        "name":"Italy", 
 
        "parent_id":2, 
 
        "lr":"," 
 
        }, 
 
        { 
 
        "id":17, 
 
        "name":"Czech", 
 
        "parent_id":2, 
 
        "lr":"," 
 
        }, 
 
        { 
 
        "id":18, 
 
        "name":"Denmark", 
 
        "parent_id":2, 
 
        "lr":"," 
 
        }, 
 
        { 
 
        "id":19, 
 
        "name":"Rest of the world", 
 
        "parent_id":2, 
 
        "lr":"," 
 
        }, 
 
        { 
 
        "id":36, 
 
        "name":"Middle East", 
 
        "parent_id":2, 
 
        "children":[ 
 
         { 
 
          "id":21, 
 
          "name":"Russia", 
 
          "parent_id":36, 
 
          "lr":"," 
 
         } 
 
        ], 
 
        "lr":"," 
 
        }, 
 
        { 
 
        "id":38, 
 
        "name":"Africa", 
 
        "parent_id":2, 
 
        "children":[ 
 
         { 
 
          "id":49, 
 
          "name":"Algeria", 
 
          "parent_id":38, 
 
          "lr":"," 
 
         }, 
 
         { 
 
          "id":50, 
 
          "name":"Morocco", 
 
          "parent_id":38, 
 
          "lr":"," 
 
         } 
 
        ], 
 
        "lr":"," 
 
        }, 
 
        { 
 
        "id":48, 
 
        "name":"Belgium", 
 
        "parent_id":2, 
 
        "lr":"," 
 
        } 
 
       ], 
 
       "lr":"," 
 
      }, 
 
      { 
 
       "id":45, 
 
       "name":"International Branch", 
 
       "parent_id":1, 
 
       "lr":"," 
 
      }, 
 
      { 
 
       "id":46, 
 
       "name":"APAC Branch", 
 
       "parent_id":1, 
 
       "children":[ 
 
        { 
 
        "id":51, 
 
        "name":"Singapore", 
 
        "parent_id":46, 
 
        "lr":"," 
 
        }, 
 
        { 
 
        "id":52, 
 
        "name":"China", 
 
        "parent_id":46, 
 
        "lr":"," 
 
        }, 
 
        { 
 
        "id":53, 
 
        "name":"Vietnam", 
 
        "parent_id":46, 
 
        "lr":"," 
 
        }, 
 
        { 
 
        "id":54, 
 
        "name":"Indonesia", 
 
        "parent_id":46, 
 
        "lr":"," 
 
        }, 
 
        { 
 
        "id":55, 
 
        "name":"India", 
 
        "parent_id":46, 
 
        "lr":"," 
 
        }, 
 
        { 
 
        "id":56, 
 
        "name":"Malaysia", 
 
        "parent_id":46, 
 
        "lr":"," 
 
        } 
 
       ], 
 
       "lr":"," 
 
      } 
 
     ], 
 
     "lr":"," 
 
     } 
 
    ] 
 
} 
 

 
function buildHierarchy(item){ 
 
    console.log('name', item.name); 
 
    if(item && item.children){   
 
     item.children.forEach(el=>buildHierarchy(el)); 
 
    } 
 
} 
 

 
buildHierarchy(data.data[0]);

+0

現在我得到了:item.children.foreach不是一個函數,因爲它正在到達一個沒有孩子的項目,但它需要繼續處理下一個項目。 – imperium2335

+1

@ imperium2335你得到這個錯誤,因爲'foreach'不是一個函數。你需要'forEach'。 –

+0

@DeividasKaržinauskas感謝您指出,愚蠢的錯誤:) –