Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

116
Views
equating last value in loop problem in innerHtml

forecasts is array who has a 10 elements when ı try to print its work perfectly when ı use innerHtml its giving the last value of items and everything is look same

const getOtherDays = (data) => {
  data.forecasts.forEach((forecast)=>{
    for(var i = 0; i < day.length; i++) {
      items = forecast.day;
      console.log(items)
      day[i].innerHTML = `${items}`
    }
    })
  }
  
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You shouldn't use a nested loop. If each element of data.forecasts is for a different day, use the index in that array to assign to the corresponding DOM element.

const getOtherDays = (data) => {
  data.forecasts.forEach((forecast, i) => {
    items = forecast.day;
    console.log(items)
    day[i].innerHTML = `${items}`
  })
}
about 4 years ago · Juan Pablo Isaza Report

0

You are looping over the elements and setting it to the forecast of the day on every one. You want to select the specific element. The forEach index has the index. Hopefully that index matches the day in your elements. Basic idea:

const getOtherDays = (data) => {
  data.forecasts.forEach((forecast, index)=>{
    items = forecast.day;
    day[index].innerHTML = `${items}`;
  })
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!