Reputation: 1920
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 one of the object's properties, let say Id
of first object ("Id":"5"). How can I do that?
Upvotes: 1
Views: 15706
Reputation: 585
var obj_c = [
[{"Id":"5","Color":"White"}],
[{"Id":"57", "Color": "Blue"}],
[{"Id":"9","Color":"Brown"}]
];
console.log(obj_c[0][0].Id);
console.log(obj_c[0][0].Color);
Upvotes: 1
Reputation: 136
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"];
Upvotes: 6
Reputation: 1467
if it's javascript your object must be named (e.g. x)
Then select the index of the first array (here : 0, 1 or 2)
Then the "small" array content only one item, you have no choice, take 0.
For end, you can pick the property you need, Id or Color.
You have :
var myColor = x[1][0]["Color"];
console.log(myColor); //output : Blue
Upvotes: 1