Problem with array.length

Hi everyone. I am a student at freeCodeCamp and recently I have started building my own apps.

I am trying to create a vaccine tracker.

In the json file, I am trying to check which countries have vaccination data.
I want to remove the countries that don’t have recent vaccination data (yesterday or today).

In the array “data”, the last item is the most recent day, therefore to check inside I am using data.length - 1, which works fine!

But when I want to check for the day before, therefore array.length - 2, that doesn’t seem to work. It goes directly to catch.

I am not very good with javascript engines, infact if I run my code in consoles it just goes to catch without giving me any other information.

What am I doing wrong?

Snippet of the code that is working:

const start = async () => {

try {

    const response = await fetch("https://covid.ourworldindata.org/data/owid-covid-data.json");

    const dataBefore = await response.json();

    const dataDelete = await function() {

        Object.keys(dataBefore).forEach(item => {

            let dataRemoval = dataBefore[item].data.length - 1;

            if (!dataBefore[item].data[dataRemoval].people_fully_vaccinated && !dataBefore[item].data[dataRemoval].people_vaccinated) {

                delete dataBefore[item]

            }

          });

    }

    dataDelete();

    createCountryList(dataBefore)

    console.log(dataBefore)

} catch(e) {

    console.log(e)

}

}

start();

Thank you in advance for your help

Because Saint Helena (SHE) has only one vaccination day available therefore you can’t access index at length - 2.

1 Like

I’m a newbie so don’t mind me I’m gonna sit right here in this corner and wait for some smart experienced person to respond -------------------> :drooling_face:

1 Like