rablentain
rablentain

Reputation: 6715

Nested javascript objects

I have some objects like this: (wordpress attachment object)

attachment.sizes (which is an object and contains:) 
                .full (each contains height and width:)
                     .height
                     .width
                .medium
                       .height
                       .width
                .thumbnail
                          .height
                          .width

And then I have another object:

props.size (which is either "full","medium" or "thumbnail")

Can I in some way write similar to

attachment.sizes.(props.size).height 

To get the height? (That did not work when I tried)

Upvotes: 1

Views: 38

Answers (1)

user229044
user229044

Reputation: 239270

Yes, using []:

attachment.sizes[props.size].height 

Both notations, a.b and a['b'], are equivalent, but only [] let you use a variable name as the property.

Upvotes: 4

Related Questions