tommyd456
tommyd456

Reputation: 10683

Can't access response from API properly

My API returns this response:

{"__v":0,"short":"8xdn4a5k","_id":"5404db5ac27408f20440babd","branches":[{"version":1,"code":""}],"ext":"js","language":"javascript"}

This works:

console.log(response.short);

but this produces undefined:

console.log(response.branches.version);

How come?

Upvotes: 0

Views: 38

Answers (2)

Well, branches has no nodes that can be reach by it's name, only an array, so you can't get it by a name (which doesn't have) , you'll have to do:

console.log(response.branches[0].version);

Indexed and Associative arrays are different.

Upvotes: 0

AstroCB
AstroCB

Reputation: 12367

The branches property of response is an array:

"branches":[{"version":1,"code":""}]

So, you have to access the first element of branches (which is the object you're looking for) to get the version property:

response.branches[0].version

Upvotes: 1

Related Questions