Arjun Ajith Arjun Ajith - 4 months ago 7
JSON Question

how to access object inside an object Array which is inside another array in javascript?

I have an array as follows

[
[{"Id":"5","Color":"White"}],
[{"Id":"57","Color":"Blue"}],
[{"Id":"9","Color":"Brown"}]
]


each object is inside an array which is inside another array. I want to access an object item, let say 'Id' of first object ("Id":"5"). How can I do that?

Answer

If the array is assigned to a variable:

var a = [
 [{"Id":"5","Color":"White"}],
 [{"Id":"57","Color":"Blue"}],
 [{"Id":"9","Color":"Brown"}]
];

You can do it like this:

a[0][0].Id;

or

a[0][0]["Id"];

To get the second object you would do:

a[1][0].Id;

or

a[1][0].["Id"];