skip to Main Content

I have 3 different Arrays(ArrayA, ArrayB, ArrayC) which I am iterating through the value of each array items line by line.

ArrayA has 20 items in it,

ArrayB has 10 items in it

ArrayC has 8 items in it.

When I iterate through each item of ArrayA to get the value of the Array, I want to use each items of the Array(ArrayA, ArrayB, ArrayC) with one another without repeating the operation for each item of the ArrayB and ArrayC.

This challenge makes me to resort to nested for loop so that I can iterate through the items of each Array but the problem is that the inner loop get executed all at once before the outer loop which is not what I want.

My code:


arrayA = [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20];

arrayB = [1,2,3,4,5,6,7,8,9,10,11,12];

arrayC = [1,2,3,4,5,6,7];


for (var i = 0; i == arrayA.length; i++) {
  console.log(arrayA[i]);

  for (var j = 1; j <= arrayB.length; j++) {
    /*I want to pick the current value of this arrayB because I will use it with arrayA current value but I don't want all the items of this current arrayB values but the for loop will finish the iteration first*/
    console.log(arrayB[j]);

    for (var k = 1; k <= arrayC.length; k++) {
      /*I want to pick the current value of this arrayC because I will use it with arrayA and arrayB current value but I don't want all the items of this arrayC values but the for loop will finish the iteration first*/
      console.log(arrayC[k]);
    }
  }
}

***WHAT I EXPECTED

What I want is that when I pick one item in arrayA, one item in arrayB and one item in arrayC and use them and iterate again from arrayA and pick the next item, to arrayB and pick the next item and to arrayC and pick the next item and do the same. The problem is that the inner loop finishes the iteration before going back to the outer loop and also because arrayB and arrayC have lesser items compare to arrayA items, their iteration finish before arrayA, once the arrayB and arrayC finish their items they should automatically start from begining of the array until arrayA finishes its own iteration.

Thank you in advance, I will appreciate your help gurus.

3

Answers


  1. You could iterate the longest array and take an adjusted index for smaller arrays.

    const
        a = [1, 2],
        b = [3, 4, 5],
        c = [6, 7, 8, 9, 0];
        
    for (let i = 0, l = Math.max(a.length, b.length, c.length); i < l; i++) {
        console.log(a[i % a.length], b[i % b.length], c[i % c.length]);
    }
    Login or Signup to reply.
  2. If I am not wrong, I think this is what you want:

    var arrayA = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20];
    var arrayB = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12];
    var arrayC = [1, 2, 3, 4, 5, 6, 7];
    
    for (var i = 0; i < arrayA.length; i++) {
      console.log("ArrayA:", arrayA[i]);
    
      for (var j = 0; j < arrayB.length; j++) {
        console.log("ArrayB:", arrayB[j]);
    
        for (var k = 0; k < arrayC.length; k++) {
          console.log("ArrayC:", arrayC[k]);
        }
      }
    }

    Let me know if I am mistaken.

    The code snippet might not show the complete console logs, so please copy it into your code editor or try it in your browser’s console.

    Login or Signup to reply.
  3. You can iterate through the largest array using a single for loop and use the modulo operator to index the smaller arrays. Here is the most Python like way to implement this in JavaScript.

    const arrayA = [1, 2, 3, 4, 5, 6, 7, 8, 9];
    const arrayB = [1, 2, 3, 4, 5, 6];
    const arrayC = [1, 2, 3];
    
    for (let [index, value] of arrayA.entries()) {
      console.log(value, arrayB[index % arrayB.length], arrayC[index % arrayC.length]);
    }
    

    Output:

    1 1 1
    2 2 2
    3 3 3
    4 4 1
    5 5 2
    6 6 3
    7 1 1
    8 2 2
    9 3 3
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search