Tim
Tim

Reputation: 95

How do I access such deeply nested object in JS? [JSON]

I can't access items list in my JSON response from server - when I try to do result.reservations.items or result.reservations.items[0] I get "undefined". How do I access it?

This is what I get after console.log of result.reservations:

[ 
 { 
    id: 1,
    reservationDetails: { price: 4970, advance: 1500, },
    items: [ [Object] ],
    client: 
     { 
       id: 8,
       clientType: 'person',
       currency: 'USD',
       guests: [Array] 
    } 
 } 
]

Upvotes: 0

Views: 49

Answers (1)

jalynn2
jalynn2

Reputation: 6457

reservations is an array of objects. Each reservation object has an attribute named 'items', which is an array of objects. So to get to the first item of the first reservation, you would use: result.reservations[0].items[0]

Upvotes: 1

Related Questions