我正在创建一个函数,该函数使用数组来导航JSON对象,在每一步将其键推送到结果数组。麻烦的是,我不能让循环在每个连续的循环中使用更新的对象。
JSON对象:
myData = {
'Eye': {
'Abnormal Morphology': [
'Neoplasm',
'Abnormality of the globe'
],
'Abnormal Physiology': [
'Hemorrhage',
'Ptosis',
'Ocular pain'
]
},
'Ear': {
'Outer Ear': [
'Abnormality of the pinna',
'Abnormal location of ear',
'Extra chondra fold'
],
'Middle Ear': [
'Glue ear',
'Otitis media'
]
}
}
功能:
view = ['Ear', 'Outer Ear']
getMenuItems(object, array) {
let menuItems = Object.keys(object);
let result = [menuItems];
for (let item in array) {
object = object[item]; // use updated object each time
menuItems = Object.keys(object);
result.push(menuItems);
}
return result;
}
我期待的结果是:
[['Eye', 'Ear'], ['Outer Ear', 'Inner Ear'], ['Abnormality of the pinna', 'Abnormal location of ear', 'Extra chondra fold']]
但我得到的只是:
['Eye', 'Ear']
答案 0 :(得分:1)