Reputation: 4160
I am sending an api call and retrieving data in json format.
$.getJSON(weatherAPI, function(data){
// get data
});
If I call the object data
and one of its properties (data.weather
) I get the following outputs
[Object {
description: "clear sky",
icon: "xyz",
main: "clear"
}]
I can't seem to use data.weather.description
to get the desired output of "clear sky"
The whole json format data below
Upvotes: 3
Views: 282
Reputation: 222522
weather is an array of Objects
, so you need to specify the index
and access the property
console.log(data.weather[0].description);
if you need to print all the element's value, use .foreach
or .map()
.map()
returns a new array while .forEach()
doesn't. forEach()
just operates on every value in the array. if you just need to console output the values use forEach
.
using forEach,
data.weather.forEach((e) => {
console.log(e.description);
});
using .map
data.weather.map((e) => {
console.log(e.description);
return e;
});
Upvotes: 6