0

Hi I have trouble with an array response of json. I'm supposed to get the members of the objects. but that array is inside another array.
This is the array that is being returned.

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]",

    }
]   
];

How can i dig down to the values? before I would use arr[i].email, but now it doesn't work. I've tried arr[0].[i].email, but returns be the error missing name after . operator. Is there a way that I can remove that outer array?

2

2 Answers 2

2

It should be arr[i][j].email. i to loop over the array arr itself and j to loop over each sub-array.

arr[i] will give you something like this (if i == 0 for example):

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

and then arr[i][j] will give something like this (if i == 0 and j == 2):

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

then you can access the email property using arr[i][j].email.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks that's so silly of me to forget that very simple array method
0

There's two ways to access objects in Javascript: using periods or using square brackets. Here, you're trying to mix the two, which works, but probably isn't the best practice. You should pick whichever is best for the situation. Here, you'd want to use the brackets:

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

Note that when using variables, you will always need to use brackets as opposed to periods.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.