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

131
Views
Loop through objects like box0, box1, box2 with a for loop

Let's say I have 5 objects, box0 box1 box2 box3 box4 box5.

Is there any way to access their members in a for loop? I tried this way, but it wouldn't work because box${i}is a string

 let boxCoordinates: Array<Coordinate> = [];
 for(let i=0;i<5;i++)
     {
         boxCoordinates.push(`box${i}`.current.getBoundingClientRect) //box0, box1, box2, box3, box4
     }
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can use eval but note that using this method may has some problems and risk for you: see this

var box0 = {index:1};

for(let i=0;i<1;i++)
   {
       let d = eval(`box${i}`);
       console.log(typeof d);//object
       console.log(d.index) //1
   }

Playground

about 4 years ago · Juan Pablo Isaza Report

0

Is there a reason to not have them all tracked in an array in the first place?

// Assuming their defined somewhere else
const boxArray = [box1, box2, box3, box4, box5];
for (let i =0; i < boxArray.length; i++ ) { 
  const box = boxArray[i];
  // Do what you need with box now
}
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!