Reputation: 1465
I have a JSON object that when I do this:
console.log(response.json);
I get this
{ results:
[ { address_components: [Object],
formatted_address: 'Google Bldg 42, 1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA',
geometry: [Object],
place_id: 'ChIJPzxqWQK6j4AR3OFRJ6LMaKo',
types: [Object] } ],
status: 'OK' }
I want to be able to select formated_address
as an example. I've tried variations of console.log(response.json.formatted_address);
that but I can't quite figure it out.
Upvotes: 0
Views: 8158
Reputation: 321
you can use this code
console.log(response.json.results[0].formatted_address);
Upvotes: 0
Reputation: 222722
Just access the first element , i.e index 0
of the array
and then formatted_address
console.log(response.json.result[0].formatted_address);
Upvotes: 1
Reputation: 707
You have an object inside an array, so you need to specify the first item in the array.
response.json.results[0].formatted_address
should work.
Upvotes: 1
Reputation: 15615
The value is not accessible directly so you need to do something like this.Your formatted_address lies in an array which is present in the result key.So to get your result do something like this
console.log(response.json.result[0].formatted_address);
Upvotes: 0