While fetching data from an API, how can I access in the response a key property that is the current date?

I am trying to obtain some data, however I can’t access to the keys property of the current date, meaning the key is for example “2023-10-03” and tomorrow will be “2023-10-04”. I tried storing the date in a variable and also using [keys[0]] but I can’t get the data.

const date = new Date();
let day = date.getDate();
let month = date.getMonth() + 1;
let year = date.getFullYear();
let currentDate = `${year}-${month}-${day}`;

async function nasaAsteroids () {
    const response = await fetch(nasaAsteroidUrl);
    const asteroidData = await response.json();
    let randomAsteroid = Math.floor(Math.random() * asteroidData.element_count); 
    // here is the problem
    console.log(asteroidData.near_earth_objects[keys[0]]);
}
nasaAsteroids();

// the json response
Asteroids - NeoWs - feed
{
  "links": {
    "next": "http://api.nasa.gov/neo/rest/v1/feed?start_date=2023-10-04&end_date=2023-10-04&detailed=false&api_key=Xqt6ZbilW49UfGA7xNEC6wTnuSdA0XGCnitavxya",
    "prev": "http://api.nasa.gov/neo/rest/v1/feed?start_date=2023-10-02&end_date=2023-10-02&detailed=false&api_key=Xqt6ZbilW49UfGA7xNEC6wTnuSdA0XGCnitavxya",
    "self": "http://api.nasa.gov/neo/rest/v1/feed?start_date=2023-10-03&end_date=2023-10-03&detailed=false&api_key=Xqt6ZbilW49UfGA7xNEC6wTnuSdA0XGCnitavxya"
  },
  "element_count": 18,
  "near_earth_objects": {
    "2023-10-03": [
      { ...

If someone can help it would be great!

It would be good to see your actual error because I’m thinking it’ll be something like “'keys 'is not defined”.

What you have above says that you want to get a value - either the first/0th entry from an Array list named keys or the value of a property/key named “0” from a Map/Object named keys. You then want use that result as a key/property to lookup in the near_earth_objects Object.

You’re referencing something named “keys” but it doesn’t exist. While Objects are made out of “key:value” pairs, there is no automatic “thing” named keys.

You can make an array of keys for any Object by using the method Object.keys(someObj) which will return an array of strings that are the keys of someObj.

const keys = Object.keys(asteroidData.near_earth_objects);

Now “keys” exists as An array which will [hopefully] have some data at keys[0], or even at keys[randomAsteroid].

Note - Technically the entries/keys in any object are not guaranteed to be in any particular sorted order.

(Also see Object.entries() and Object.values() for some other ways to handle dealing with Objects’ data. )

Hi,

Thank you for your response. The console always showed undefined. However I solved it, creating a variable from the API response.

The solution was:

    let currentDate = Object.keys(asteroidData.near_earth_objects);

    asteroidName.textContent = asteroidData.near_earth_objects[`${currentDate}`][randomAsteroid].name;

The nasa Asteroid Url variable remains to be defined. It is not shown how this variable is defined in the code you provided

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.