I need to access the "State" value from the following array --
data =
{
Images:
[
{
ProductCodes: [],
BlockDeviceMappings: [Object],
Tags: [],
ImageId: 'ami-75301c',
ImageLocation: '54696560/Test Image 3',
State: 'available',
VirtualizationType: 'pavirtul',
Hypervisor: 'xen'
}
],
requestId: '2eb809d3-7f82-4142-b5d1-6af3'
}
When I try data.Images["State"] or data.Images.State I get undefined.
Thanks
asked Jul 22, 2013 at 18:07
Fox
9,47413 gold badges46 silver badges63 bronze badges
3 Answers 3
Images maps to an array which stores objects, so you have to specify the index of the item you want. Try data.images[0]["State"].
Sign up to request clarification or add additional context in comments.
1 Comment
Joe
You should probably check if there are any array elements prior to getting the state item inside that array. An array could be empty.
You can access like this:
data.Images[0].State
Or even:
data.Images[0]['State']
DINA TAKLIT
8,51410 gold badges85 silver badges92 bronze badges
Comments
Access the state with data.image[0].state. Your method was wrong because inside the image, you need an index within the two square bracket, the image property is an array.
gre_gor
6,66112 gold badges105 silver badges106 bronze badges
answered Jun 21, 2017 at 17:10
naveen kumar
1883 silver badges12 bronze badges
Comments
lang-js
Imageshas noStateproperty. If you indent your structure cleanly, things may become clear.Imagesis referencing an Array, and an Array is an ordered collection of data accessible via 0-based indices.