2016-12-28 88 views
0

這是我的目標:遍歷內嵌套的對象

family_Data = { 
    "Family": [ 
     { 
      "id": "Family-15", 
      "members": 
      { 
       "Family-15-01": [{"Family Code": "Family-15-01","Gender": "Female", "Full Name" : "Evelyn Osei"}], 
       "Family-15-02": [{"Family Code": "Family-15-02","Gender": "Male", "Full Name" : "David Will"}], 
       "Family-15-03": [{"Family Code": "Family-15-03","Gender": "Male", "Full Name" : "Edwin Pan"}] 
      } 
     } 
    ] 
} 

我想遍歷「成員」的對象。

+1

瑣碎的使用嵌套的'$ .each',但你嘗試過什麼? –

+0

提示:'family_Data.Family [0] .members'指的是你想循環的對象。 –

+0

您可以使用'for(var key in family_Data.Family [0] .members)'循環獲取所有鍵的對象 – VadimB

回答

0

var family_Data = { 
 
"Family": [ 
 
{ 
 
"id": "Family-15", 
 
"members": { 
 
"Family-15-01": [{"Family Code": "Family-15-01","Gender": "Female", "Full Name" : "Evelyn Osei"}], 
 
"Family-15-02": [{"Family Code": "Family-15-02","Gender": "Male", "Full Name" : "David Will"}], 
 
"Family-15-03": [{"Family Code": "Family-15-03","Gender": "Male", "Full Name" : "Edwin Pan"}] 
 
} 
 
} 
 
] 
 
}; 
 
for(var key in family_Data.Family[0].members){ 
 
    console.log(family_Data.Family[0].members[key]); 
 
    }

+0

太棒了!非常感謝。 – Drew

0

首先得到您的members對象。

var members = family_Data.Family[0].members; 

然後遍歷該對象。

for(var key in members){ 
    var member = members[key]; 
    // here we go. 
} 
+0

Subash,您的代碼可以正常工作,但是您可以幫助我刪除Family代碼。我想要這樣的對象:「成員」:{{「家庭代碼」:「家庭15-01」,「性別」:「女性」,「全名」:「Evelyn Osei」}], [ {「家庭代碼」:「家庭15-02」,「性別」:「男性」,「全名」:「大衛威爾」}], [{「家庭代碼」:「家庭15-03」, 「性別」:「男性」,「全名」:「Edwin Pan」}] } – Drew

+0

我已將自己排序爲Subash。這就是我所做的。 var result = {}; member.forEach(x => {x.que] = x.ans; }); – Drew