I have two arrays – array1
and array2
as shown in the code snippet below. I want to store following information in a message variable such that I could use that in a jQuery dialog.
So, in case of following arrays, I want have my message variable hold following information:
1 . File ABC012345 is located inside BOX#9
2. File DEF06789 is located inside BOX#10
var array1 = [
"ABC012345",
"DEF06789"
]
var array2 = [
"BOX#9",
"BOX#10"
]
let messageOne = "1.File "+array1[0]+" is located inside "+array2[0];
let messageTwo = "2.File "+array1[1]+" is located inside "+array2[1];
//console.log(messageOne);
//console.log(messageTwo);
for (i = 0 ; i < array1.length; i++){
for (j = 0 ; j < array2.length ; j ++) {
let message = "File "+array1[i]+" is located inside "+array2[j];
console.log(message);
}
}
But the way I’m attempting to do is producing incorrect results. Is there a better / correct way of approachin it?
2
Answers
You don’t need to nested loop for this case, you can do it in just 1 loop
You do not need nested loops here. Instead, you should use a single loop and index both arrays at the same time.
Demo: