how to render diff elements at once in vanila js
i have this api data what i want to have 7 diff p tags and every p tags should have maxtemp_c value how to achive this
7 Replies
Assuming that's an array of data, you could do something like this:
index.js:48 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'maxtemp_c')
You have to wait for the promise to resolve
let arr=[]
const container = document.getElementById("weather-up");
const getForecast = async(city)=>{
let res = await fetch(
let data = await res.json(); arr.push(data.forecast.forecastday); const toInject = arr.map((el)=>{ const p = document.createElement('p'); p.textContent=
https://api.weatherapi.com/v1/forecast.json?key=cb543f98c304497393a70530232306&q=${city}&days=7&aqi=no&alerts=no
);let data = await res.json(); arr.push(data.forecast.forecastday); const toInject = arr.map((el)=>{ const p = document.createElement('p'); p.textContent=
${el.day.maxtemp_c}
;
return p;
})
container.append(...toInject)
}
my whole codeYou’ll have to log the data, make sure it’s the way you expect it and make changes as needed
I’m at work so I can’t be that helpful right now. Won’t be home for about 7hrs
No worries i'll figure it out
Ok, so I found the error: you're pushing the entire
data.forecast.forecastday
array into only one element of arr
so there's nothing to loop over.
Don't use an intermediate array, instead:
That way you map over the array given.
In addition, for the future, please use codeblock syntax when sharing code, it makes it a lot easier to read as well as copy/paste. See #How To Ask Good Questions for the syntax.