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

170
Views
How to assign a variable for each item of the loop
for (var i = 0; i < dataForecast.list.length; i += 8) {
  const data = dataForecast.list[i].dt_txt;
}

This loops gives out 5 intended looped pieces of data but I'm struggling to assign each loop to its own variable.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Use an array

const dataArr = [];
for (var i = 0; i < dataForecast.list.length; i += 8) {
  const data = dataForecast.list[i].dt_txt;
  dataArr.push(data);
}
console.log(dataArr); // access the array
about 4 years ago · Juan Pablo Isaza Report

0

It seems you want to dynamically generate and assign variable value. For this you can use eval() method like below:

dataArray = [1, 2, 3, 4, 5]
var x = 'data'

for (var i = 0; i < dataArray.length; i++) {
  eval('var ' + x + i + '= ' + dataArray[i] + ';');
}
console.log("data0=" + data0);
console.log("data1=" + data1);
console.log("data2=" + data2);
console.log("data3=" + data3);
console.log("data4=" + data4);

Note: It's not recommended to use eval() method due to security reasons. You can read more at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval

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!