Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

134
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda