2017-02-04 164 views
0

嗨我有問題的數組響應的JSON。 我應該得到的對象的成員。但該數組在另一個數組內。
這是返回的數組。如何獲得數組內的數組?

var arr = [ 
[ 
    { 
     "id": 4243430853, 
     "email": "[email protected]", 
    }, 
    { 
     "id": 4227666181, 
     "email": "[email protected]", 

    }, 
    { 
     "id": 4227644293, 
     "email": "[email protected]", 

    } 
], 
[ 
    { 
     "id": 4243430854, 
     "email": "[email protected]", 
    }, 
    { 
     "id": 4227666182, 
     "email": "[email protected]", 

    }, 
    { 
     "id": 4227644294, 
     "email": "[email protected]", 

    } 
] 
]; 

我該如何挖掘價值?之前我會使用arr[i].email,但現在它不起作用。我嘗試過arr [0]。[i] .email,但返回的錯誤是missing name after . operator。有沒有辦法可以刪除外部數組?

+0

只要刪除括號內的'.'。 – Xufox

+0

可能的重複[Javascript錯誤名稱丟失後。運算符在變量函數](http://stackoverflow.com/questions/16172526/javascript-error-missing-name-after-operator-on-variable-function) – Xufox

回答

2

它應該是arr[i][j].emaili以遍歷數組arr本身和j循環遍歷每個子陣列。

arr[i]會給你這樣的事情(如果i == 0爲例):

[ 
    { 
     "id": 4243430853, 
     "email": "[email protected]", 
    }, 
    { 
     "id": 4227666181, 
     "email": "[email protected]", 
    }, 
    { 
     "id": 4227644293, 
     "email": "[email protected]", 
    } 
] 

然後arr[i][j]會給這樣的事情(如果i == 0j == 2):

{ 
    "id": 4227644293, 
    "email": "[email protected]", 
} 

那麼你可以使用arr[i][j].email訪問email財產。

+0

謝謝,這是我很愚蠢忘記,非常簡單的數組方法 – yok2xDuran

0

有兩種方式訪問​​Javascript中的對象:使用句號或使用方括號。在這裏,你試圖混合兩者,這可行,但可能不是最佳做法。你應該選擇最適合的情況。在這裏,你會想用括號:

arr[i][j]["email"]; 

注意,使用變量的時候,你總是會需要使用括號,而不是時間。