2017-02-04 89 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"]; 

注意,使用变量的时候,你总是会需要使用括号,而不是时间。