I'm encountering a challenge when trying to access nested keys within an object. Specifically, I am attempting to display the "kilometers_per_second" values found in the "relative_velocity" section under "close_approach_data".
Despite my efforts, I keep running into errors and can't seem to pinpoint where I'm going wrong. Any assistance would be greatly appreciated.
const asteroidData = near_earth_objects
.map((data) => data.close_approach_data[0])
.map((e) => e.relative_velocity);
// TypeError: Cannot read property 'relative_velocity' of undefined
or
const asteroidData = near_earth_objects
.map((data) => data.close_approach_data[0].relative_velocity);
// TypeError: Cannot read property 'relative_velocity' of undefined
Here is the JSON object structure:
"near_earth_objects": [
{
"links": {
"self": "http://www.neowsapp.com/rest/v1/neo/2021277?api_key=24TE7EgNfmXIvdb6vNNZGBWx8s54XbZzCCi2oAdN"
},
"id": "2021277",
"neo_reference_id": "2021277",
"name": "21277 (1996 TO5)",
"designation": "21277",
"nasa_jpl_url": "http://ssd.jpl.nasa.gov/sbdb.cgi?sstr=2021277",
"absolute_magnitude_h": 16.1,
"estimated_diameter": {
"kilometers": {
"estimated_diameter_min": 1.6016033798,
"estimated_diameter_max": 3.5812940302
},
"meters": {
"estimated_diameter_min": 1601.6033797856,
"estimated_diameter_max": 3581.2940301941
},
"miles": {
"estimated_diameter_min": 0.9951898937,
"estimated_diameter_max": 2.2253122528
},
"feet": {
"estimated_diameter_min": 5254.6044325359,
"estimated_diameter_max": 11749.652706022
}
},
"is_potentially_hazardous_asteroid": false,
"close_approach_data": [
{
"close_approach_date": "1945-06-07",
"close_approach_date_full": "1945-Jun-07 22:35",
"epoch_date_close_approach": -775272300000,
"relative_velocity": {
"kilometers_per_second": "15.5094751879",
"kilometers_per_hour": "55834.1106763388",
"miles_per_hour": "34693.1450477507"
},
"miss_distance": {
"astronomical": "0.0334232973",
"lunar": "13.0016626497",
"kilometers": "5000054.084456751",
"miles": "3106889.5396991238"
},
"orbiting_body": "Mars"
}
],
"is_sentry_object": false
},
// Object continues repeating the above structure