Alex Nordhausen
Alex Nordhausen

Reputation: 93

Get Immediate Child Property in Javascript Object

I am working with a minimally documented API. The response returns an object and, within that object, I have data I need...

i.e.

response: {
   locations: {
      location: {
         21421: { <-- this is random
           ...data needed in here
         }
      }
   }
}

The 21421 property appears to be a random number. I have no idea why, again, the documentation is very sparse. Is there any way to get the immediate child property of the response.locations.location? If so, that would greatly help me.

Thanks!

Upvotes: 1

Views: 75

Answers (1)

Joseph Marikle
Joseph Marikle

Reputation: 78550

You can iterate over it using for...in

for(var prop in response.locations.location) {
    console.log(response.locations.location[prop])
}

Upvotes: 1

Related Questions