2010-03-01 160 views
0

報警變量在JavaScript它顯示了這樣的時候.. 共9個值(名稱,地址,城市,朋友等)獲取價值

如何以檢索值

[對象的對象],[對象對象],[對象對象],[對象對象],[對象對象],[對象對象],[對象對象],[對象對象],[對象對象]

回答

5

看起來像數組對象,您需要循環訪問每個對象

for(var x in thatObjectArray){ 
    doSomethingOn(thatObjectArray[x].name); 
    doSomethingOn(thatObjectArray[x].address); 
    ... 
} 

或者測試,你可以像alert(thatObjectArray[0].name)

3

JSON.stringify()和.toSource()方便查看[object Object]。

REF: Implementing Mozilla's toSource() method in Internet Explorer

javascript: 
    objRA=[ 
    {name:"Fred",city:"N.Y",friend:"Mike"}, 
    {name:"Dave",city:"S.F",friend:"Mike"}, 
    {name:"Dave",city:"S.F",friend:"Fred"}, 
    {name:"Mary",city:"S.F",friend:"Fred"}, 
    {name:"Mary",city:"S.F",friend:"Dave"}, 
    {name:"Dave",city:"S.F",friend:"Mary"}, 
    {name:"Fred",city:"N.Y",friend:"Mary"}, 
]; 
alert(JSON.stringify(objRA)); 

顯示器

[{"name":"Fred","city":"N.Y","friend":"Mike"}, 
{"name":"Dave","city":"S.F","friend":"Mike"}, 
{"name":"Dave","city":"S.F","friend":"Fred"}, 
{"name":"Mary","city":"S.F","friend":"Fred"}, 
{"name":"Mary","city":"S.F","friend":"Dave"}, 
{"name":"Dave","city":"S.F","friend":"Mary"}, 
{"name":"Fred","city":"N.Y","friend":"Mary"}] 

但是,如果...

javascript: 
    objRA=[ 
     {name:"Mike"}, 
     {name:"Fred",city:"N.Y",friend: "?" }, 
     {name:"Dave",city:"S.F",friend: "?" }, 
     {name:"Dave",city:"S.F",friend: "?" }, 
     {name:"Mary",city:"S.F"}, 
     {name:"Mary",city:"S.F",friend: "?" }, 
     {name:"Dave",city:"S.F",friend: "?" }, 
     {name:"Fred",city:"N.Y"}, 
]; 
    objRA[1].friend=objRA[0]; 
    objRA[2].friend=objRA[0]; 
    objRA[3].friend=objRA[1]; 
    objRA[4].friend=objRA[1]; 
    objRA[5].friend=objRA[2]; 
    objRA[6].friend=objRA[4]; 
    objRA[7].friend=objRA[4]; 
alert(objRA.toSource().split("},").join("},\n")); 

然後僅.toSource()中可以顯示(一個小的手動勸導):

 
    [#1={name:"Mike"}, 
    #2={name:"Fred", city:"N.Y", friend:#1#}, 
    #3={name:"Dave", city:"S.F", friend:#1#}, 
     {name:"Dave", city:"S.F", friend:#2#}, 
    #4={name:"Mary", city:"S.F", friend:#2#}, 
     {name:"Mary", city:"S.F", friend:#3#}, 
     {name:"Dave", city:"S.F", friend:#4#}, 
     {name:"Fred", city:"N.Y", friend:#4#}]