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

200
Visualizações
How can I pass multiple const's of .entries into one "for" loop

I have this code:

for (const [index, value] of txtfile1.entries()) {
  console.log(`${index}: ${value}`);
}

And I need to pass this loop one more txt file to make something like this:

for (const [index, value] of txtfile1.entries()) && (const [index2, value2] of txtfile2.entries()) {
  console.log(`${index}: ${value} and ${index2}: ${value2}`);
}

So my goal is to use data from two different txt files in the same loop. Pls help!

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

you can't loop over multiple things directly in a for loop. Use a loop that provides indexes, then use that to index into both arrays.

const e1 = txtfile1.entries();
const e2 = txtfile2.entries();
e1.forEach(([key, value], index) => {
  const [key2, value2] = e2[index];
  console.log(`${key}: ${value} and ${key2}: ${value2}`);
});
about 4 years ago · Juan Pablo Isaza Relatório

0

Maybe you can take longest entries and loop like this

const e1 = ["A1","A2","A3"]
const e2 = ["B1","B2"];
const longest = e1.length > e2.length ? e1 : e2
for (const [index] of longest.entries()) {
  console.log(`${index}: ${e1[index] || ""} and ${index}: ${e2[index] || ""}`);
}

output:

0: A1 and 0: B1 
1: A2 and 1: B2 
2: A3 and 2:  
about 4 years ago · Juan Pablo Isaza Relatório

0

So I've got a comment by Jupri: cant you just use nested :

for (const [index, value] of txtfile1.entries()) for (const [index2, value2] of txtfile2.entries()) {console.log(${index}: ${value} and ${index2}: ${value2})}

And it worked, thanks a lot to Jupri and everyone else who responded.

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