Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I´m trying to looping through a Json... to get a new var = an array made up of months values [enero,febrero,marzo,etc] but I CANT DO IT, because console keeps telling me : Uncaught ReferenceError: data is not defined.

Json:

"mes": "Enero",
"T. media (C)": 10.5,
"T. min (C)": 6.5,
"T. max (C)": 15.1,
"Precipitacion (mm)": 10.3,
"Humedad (%)": 74,
"Dias Luviosos (dias)": 10,
"Horas de Sol (h)": 6.1
},
etc etc etc

this is the code for main.js


async function todo() {
const consulta = await fetch("https://raw.githubusercontent.com/br444ndy/c8/main/datos.json");
const data = await consulta.json();
console.log(data);
}

var meses = [];

data.forEach((m) => {
meses.push(m.mes());
});

console.log(meses);

What I have tried:

heeeeeeelps i cant even understand where is the problem :((( im just starting
Posted
Updated 9-May-22 6:44am
Comments
Richard MacCutchan 9-May-22 4:04am    
You never call the todo function, so there is no data. But even if you do, the variable data only exists inside that function.
br444ndy 9-May-22 11:21am    
thank uu so muchhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh <3

1 solution

Shouldn't data.forEach be inside todo function,
async function todo() {
const consulta = await fetch("https://raw.githubusercontent.com/br444ndy/c8/main/datos.json");
const data = await consulta.json();
console.log(data);
var meses = [];
data.forEach((m) => {
meses.push(m.mes());
});
}

Hope it helps.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900