0

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!

dev
  • 11
  • 2
  • 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})` ? – Jupri Jun 07 '22 at 05:01

3 Answers3

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}`);
});
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • Thanks for your response! Here it seems that key and value doesn't work in console log, they show up like not in use. And also later I'm using "if" with "continue" parameter, so the latest broke with this error: "Illegal continue statement: no surrounding iteration statement". I suppose that "continue" doesn't work in forEach loops? – dev Jun 07 '22 at 04:44
  • p.s. renamed key into index in my code – dev Jun 07 '22 at 04:47
  • There's no `continue` or `break` in `forEach()`. To continue, just return from the callback function. See https://stackoverflow.com/questions/2641347/short-circuit-array-foreach-like-calling-break for how to break – Barmar Jun 07 '22 at 15:00
  • `[key, value]` and `index` have to be in the order I showed. – Barmar Jun 07 '22 at 15:19
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:  
Ibid Athoillah
  • 95
  • 2
  • 10
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.

dev
  • 11
  • 2
  • Your answer didn't explain the txtfile2 and txtfile1 structures. Also, the code shared didn't run in browser or Nodejs without adjustments. I suggest you add a brief description of the structure or txtfile1 and txtfile2 and indicate the expected output. I adjusted your code considering textfile1 and txtfile2 are simple objects: Giving const txtfile1 = { "a":"b" } and const txtfile2 = { "b": "c", "d": "e"} the code: ```javascript for (const [index, value] of Object.entries(txtfile1)) for (const [index2, value2] of Object.entries(txtfile2)) {console.log(`${index}: ${value} and ${index2}: ${va – Anderson Marques Jun 07 '22 at 10:07
  • @AndersonMarques yes you are correct, it better give sample data and expected output in question. the code is working btw, just the quotes ( ` ` ) is missing. for (const [index, value] of txtfile1.entries()) for (const [index2, value2] of txtfile2.entries()) console.log(` ${index}: ${value} and ${index2}: ${value2}` ) – Ibid Athoillah Jun 13 '22 at 08:35