2017-09-13 189 views
-3

我有這個JSON數據與我,我想從每個孩子得到slu value的價值。從JSON數據中獲取價值

主要問題是我不知道每次獲得新數據後會生成多少孩子。總之,小孩內部的一代孩子是動態的,並不是固定的。

那麼,如何從JSON數據中存在的每個孩子獲得slu value的值?

在這裏,我寫一個JSON數據:

[ 
    { 
     "id": 11, 
     "title": "Bottle", 
     "__domenu_params": {}, 
     "href": "www.products.com", 
     "target": "_blank", 
     "slug": "/undefined" 
    }, 
    { 
     "id": 10, 
     "title": "Pencils", 
     "__domenu_params": {}, 
     "slug": "/Pencils" 
    }, 
    { 
     "id": 9, 
     "title": "Stationary", 
     "__domenu_params": {}, 
     "slug": "/Stationary" 
    }, 
    { 
     "id": 8, 
     "title": "Pen", 
     "__domenu_params": {}, 
     "slug": "/Pen" 
    }, 
    { 
     "id": 7, 
     "title": "Cable", 
     "__domenu_params": {}, 
     "slug": "/Cable" 
    }, 
    { 
     "id": 5, 
     "title": "Electronics", 
     "__domenu_params": {}, 
     "slug": "/Electronics", 
     "children": [ 
      { 
       "id": 4, 
       "title": "Charger", 
       "__domenu_params": {}, 
       "slug": "/Charger" 
      }, 
      { 
       "id": 3, 
       "title": "Laptop", 
       "__domenu_params": {}, 
       "slug": "/Laptop" 
      }, 
      { 
       "id": 2, 
       "title": "Mobile", 
       "__domenu_params": {}, 
       "slug": "/Mobile", 
       "href": "www.electronics.com", 
       "target": "_blank", 
       "children": [ 
        { 
         "id": 6, 
         "title": "Pendrive", 
         "__domenu_params": {}, 
         "slug": "/Pendrive", 
         "href": "www.pendrive.com", 
         "target": "_self" 
        } 
       ] 
      } 
     ] 
    } 
] 

我嘗試這個代碼,並從僅此JSON得到塞的價值。我應該怎麼做才能獲得所有可能的JSON數據?

let data = [{"id":11,"title":"Bottle","__domenu_params":{},"href":"www.products.com","target":"_blank","slug":"/undefined"},{"id":10,"title":"Pencils","__domenu_params":{},"slug":"/Pencils"},{"id":9,"title":"Stationary","__domenu_params":{},"slug":"/Stationary"},{"id":8,"title":"Pen","__domenu_params":{},"slug":"/Pen"},{"id":7,"title":"Cable","__domenu_params":{},"slug":"/Cable"},{"id":5,"title":"Electronics","__domenu_params":{},"slug":"/Electronics","children":[{"id":4,"title":"Charger","__domenu_params":{},"slug":"/Charger"},{"id":3,"title":"Laptop","__domenu_params":{},"slug":"/Laptop"},{"id":2,"title":"Mobile","__domenu_params":{},"slug":"/Mobile","href":"www.electronics.com","target":"_blank","children":[{"id":6,"title":"Pendrive","__domenu_params":{},"slug":"/Pendrive","href":"www.pendrive.com","target":"_self"}]}]}] 


for (let i = 0; i< data.length ; i++) { 
    // console.log(data[i]["children"]) 
    if (data[i]["children"]) { 
     console.log("inside") 
     for (let j = 0; j < data[i]["children"].length; j++) { 
      // console.log(data[i]["children"][j]) 
      if (data[i]["children"][j]["children"]) { 
       console.log(data[i]["children"][j]["children"]) 
      } 
     } 
    } 
} 
+2

我們不鼓勵那些僅僅從背景中提出問題的帖子,並期望社羣解決它。假設你試圖自己解決它並陷入困境,那麼如果你寫下了你的想法和你無法想象的東西,這可能會有所幫助。它肯定會爲你的帖子提供更多的答案。在此之前,這個問題將被投票停止/降低投票。 – Cerbrus

+0

@Cerbrus現在好嗎? –

+0

檢查對象是否包含'children'數組。如果是這樣,則使用遞歸來再次將該子數組解析爲數據。 – tbking

回答

1

您可以使用遞歸來輕鬆地分析這些數據:

let data = [{"id":11,"title":"Bottle","__domenu_params":{},"href":"www.products.com","target":"_blank","slug":"/undefined"},{"id":10,"title":"Pencils","__domenu_params":{},"slug":"/Pencils"},{"id":9,"title":"Stationary","__domenu_params":{},"slug":"/Stationary"},{"id":8,"title":"Pen","__domenu_params":{},"slug":"/Pen"},{"id":7,"title":"Cable","__domenu_params":{},"slug":"/Cable"},{"id":5,"title":"Electronics","__domenu_params":{},"slug":"/Electronics","children":[{"id":4,"title":"Charger","__domenu_params":{},"slug":"/Charger"},{"id":3,"title":"Laptop","__domenu_params":{},"slug":"/Laptop"},{"id":2,"title":"Mobile","__domenu_params":{},"slug":"/Mobile","href":"www.electronics.com","target":"_blank","children":[{"id":6,"title":"Pendrive","__domenu_params":{},"slug":"/Pendrive","href":"www.pendrive.com","target":"_self"}]}]}] 


function getAllSlugs(categories) { 
    // For each category... 
    return categories.reduce((slugList, category) => { 
    // add the slug for the particular category... 
    slugList.push(category.slug); 
    // and, check if the category has children... 
    if (category.children && category.children.length) { 
     // if children are there, call the same function with the 
     // children and add the slugs of children 
     slugList = slugList.concat(getAllSlugs(category.children)); 
    } 
    return slugList; 
    }, []); 
} 
// Call the function 
getAllSlugs(data); 

輸出:

[ '/undefined', 
    '/Pencils', 
    '/Stationary', 
    '/Pen', 
    '/Cable', 
    '/Electronics', 
    '/Charger', 
    '/Laptop', 
    '/Mobile', 
    '/Pendrive' ] 

希望這有助於!