2016-11-04 82 views
1

我有一個包含多個子對象的對象,我想檢索所有元素。 當運行下面的代碼,我只檢索要素的一部分,直到「年齡」如何用子對象讀取對象

var output = ''; 
 
    
 
    var main_table = { 
 
    \t \t animal: 'dog', 
 
    \t \t color:'black', 
 
    \t \t age: { 
 
       year:2016, 
 
       month:11, 
 
       day:1 
 
      }, 
 
    \t \t race:'sheepdog', 
 
    \t \t parents: { 
 
       father:'Dad', 
 
    \t \t \t mother:'Mom' 
 
      } 
 
}; 
 
    
 
function test(main_table){ 
 
    table=main_table; 
 
    for (var name in table) { 
 
     if (table.hasOwnProperty(name)) { 
 
     if (table[name]=="[object Object]") { 
 
      test(table[name]); 
 
     } 
 
     else { 
 
      output+=(name+' : '+table[name]+' '); 
 
     } 
 
     } 
 
    } 
 
    alert (output); 
 
} 
 

 
test(main_table)

一些關於它的幫助將得到高度讚賞。

+1

[通過嵌套JavaScript對象迭代]的可能的複製(HTTP:// stackoverflow.com/questions/8085004/iterate-through-nested-javascript-objects) – Weedoze

回答

2

您創造了一個隱含的全局變量這一行:

table=main_table; 

被遺漏了var

我也重構了一點點,以在每個遞歸階段返回output,並在末尾返回alert

var main_table = { 
 
    \t \t animal: 'dog', 
 
    \t \t color:'black', 
 
    \t \t age: 
 
    \t \t { 
 
       year:2016, 
 
       month:11, 
 
       day:1 
 
      }, 
 
    \t \t race:'sheepdog', 
 
    \t \t parents: 
 
    \t  { 
 
       father:'Dad', 
 
    \t \t \t mother:'Mom'} 
 
    \t \t }; 
 
    
 
function test(main_table){ 
 
    var table=main_table; 
 
    var output = ''; 
 
    for (var name in table) 
 
    { 
 
     if (table.hasOwnProperty(name)) 
 
     { 
 
     console.log(name, typeof table[name]) 
 
     if (typeof table[name]== "object") 
 
     { 
 
      output+=test(table[name]); 
 
     } 
 
     else 
 
     { 
 
      output+=(name+' : '+table[name]+' '); 
 
     } 
 
     } 
 
    } 
 
    return output; 
 
} 
 

 
alert(test(main_table))

0

我建議使用迭代,在鍵和遞歸,在孩子的方式,以適當的檢查

if (object[key] !== null && typeof object[key] === 'object') { //... 

的迭代對象。

方法使用:

function getElements(object) { 
 
    var result = []; 
 
    Object.keys(object).forEach(function (key) { 
 
     if (object[key] !== null && typeof object[key] === 'object') { 
 
      result = result.concat(getElements(object[key])); 
 
      return; 
 
     } 
 
     result.push([key, object[key]]); 
 
    }); 
 
    return result; 
 
} 
 

 
var main_table = { animal: 'dog', color: 'black', age: { year: 2016, month: 11, day: 1 }, race: 'sheepdog', parents: { father: 'Dad', mother: 'Mom' } }; 
 

 
console.log(getElements(main_table));
.as-console-wrapper { max-height: 100% !important; top: 0; }

0

嗨設置了錯誤的範圍,你的功能,因爲這條線table=main_table;

這個代碼將工作的我想:

var output = ''; 

var main_table = { 
     animal: 'dog', 
     color:'black', 
     age: 
      {year:2016,month:11,day:1}, 
     race:'sheepdog', 
     parents: 
      {father:'Dad', 
      mother:'Mom'} 
     }; 

function test(table){ 
for (var name in table) 
    { 
    if (table.hasOwnProperty(name)) 
     { 
     if (table[name]=="[object Object]") 
      { 
      test(table[name]); 
      } 
     else 
      { 
      output+=(name+' : '+table[name]+' '); 
      } 
     } 
    } 
alert(output); 
} 

test(main_table);