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

192
Views
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 answers
Answer question

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 Report

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 Report

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 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!