This is my array
let result=[
{ lon: -122.08, lat: 37.39 },
[ { id: 800, main: 'Clear', description: 'clear sky', icon: '01d' } ],
'stations',
{
temp: 282.55,
feels_like: 281.86,
temp_min: 280.37,
temp_max: 284.26,
pressure: 1023,
humidity: 100
},
16093,
{ speed: 1.5, deg: 350 },
{ all: 1 },
1560350645,
{
type: 1,
id: 5122,
message: 0.0139,
country: 'US',
sunrise: 1560343627,
sunset: 1560396563
},
-25200,
420006353,
'Mountain View',
200
]
I want to fetch icon property, so I tried this
for (let i in result) {
if (i == 1) {
console.log(result[i]['icon']);
}
}
But I got "undefined" as an output. please help me access the icon property in this array.
It is not recommended to use for..in with arrays. As result is an array, then you can use index to fetch the icon property as:
result[1][0].icon
let result = [
{ lon: -122.08, lat: 37.39 },
[{ id: 800, main: "Clear", description: "clear sky", icon: "01d" }],
"stations",
{
temp: 282.55,
feels_like: 281.86,
temp_min: 280.37,
temp_max: 284.26,
pressure: 1023,
humidity: 100,
},
16093,
{ speed: 1.5, deg: 350 },
{ all: 1 },
1560350645,
{
type: 1,
id: 5122,
message: 0.0139,
country: "US",
sunrise: 1560343627,
sunset: 1560396563,
},
-25200,
420006353,
"Mountain View",
200,
];
const res = result[1][0].icon;
console.log(res);