I recently wrote a recursive function to create all combinations of arrays. You would have to translate your data into an array of arrays that my function uses, but that shouldn't be difficult.

Anyway, here's the code with a runnable example:

var v = [['Miniors','Kadettes','Juniors', 'Seniors'], ['Boys','Girls','Men','Women'],['54kg - 62kg','64kg - 70kg','71kg - 78kg','79kg - 84kg']];
var combos = createCombinations(v);
for(var i = 0; i < combos.length; i++) {
  document.getElementsByTagName("body")[0].innerHTML += combos[i] + "<br/>";
}

function createCombinations(fields, currentCombinations) {
  //prevent side-effects
  var tempFields = fields.slice();

  //recursively build a list combinations
  var delimiter = ' | ';
  if (!tempFields || tempFields.length == 0) {
    return currentCombinations;
  }
  else {
    var combinations = [];
    var field = tempFields.pop();

    for (var valueIndex = 0; valueIndex < field.length; valueIndex++) {
      var valueName = field[valueIndex];

      if (!currentCombinations || currentCombinations.length == 0) {
        var combinationName = valueName;
        combinations.push(combinationName);
      }
      else {
        for (var combinationIndex = 0; combinationIndex < currentCombinations.length; combinationIndex++) {
          var currentCombination = currentCombinations[combinationIndex];
          var combinationName = valueName + delimiter + currentCombination;
          combinations.push(combinationName);
        }
      }
    }
    return createCombinations(tempFields, combinations);
  }
}

Answer from Dave on Stack Overflow
🌐
freeCodeCamp
freecodecamp.org › news › nesting-for-loops-in-javascript
Nesting For Loops in JavaScript
June 2, 2020 - If you're having trouble understanding freeCodeCamp's Nesting For Loops challenge, don't worry. We got your back. In this problem you have to complete the multiplyAll() function, and takes a multi-dimensional array as an argument. Remember that a mul...
Top answer
1 of 2
5

I recently wrote a recursive function to create all combinations of arrays. You would have to translate your data into an array of arrays that my function uses, but that shouldn't be difficult.

Anyway, here's the code with a runnable example:

var v = [['Miniors','Kadettes','Juniors', 'Seniors'], ['Boys','Girls','Men','Women'],['54kg - 62kg','64kg - 70kg','71kg - 78kg','79kg - 84kg']];
var combos = createCombinations(v);
for(var i = 0; i < combos.length; i++) {
  document.getElementsByTagName("body")[0].innerHTML += combos[i] + "<br/>";
}

function createCombinations(fields, currentCombinations) {
  //prevent side-effects
  var tempFields = fields.slice();

  //recursively build a list combinations
  var delimiter = ' | ';
  if (!tempFields || tempFields.length == 0) {
    return currentCombinations;
  }
  else {
    var combinations = [];
    var field = tempFields.pop();

    for (var valueIndex = 0; valueIndex < field.length; valueIndex++) {
      var valueName = field[valueIndex];

      if (!currentCombinations || currentCombinations.length == 0) {
        var combinationName = valueName;
        combinations.push(combinationName);
      }
      else {
        for (var combinationIndex = 0; combinationIndex < currentCombinations.length; combinationIndex++) {
          var currentCombination = currentCombinations[combinationIndex];
          var combinationName = valueName + delimiter + currentCombination;
          combinations.push(combinationName);
        }
      }
    }
    return createCombinations(tempFields, combinations);
  }
}

2 of 2
4
function iterate(lists, fn)
{
  var values = [];
  function process(listIndex)
  {
    var list = lists[listIndex];

    // no list? create the value
    if (!list)
    {
      fn.apply(null, values);
      return;
    }

    for (var i = 0; i < list.length; i++)
    {
      values[listIndex] = list[i];
      process(listIndex+1);
    }
  }

  process(0);
}

here is a working example based on the data mentioned in your question: http://jsbin.com/boqucu/2/edit

People also ask

What is a nested loop in JavaScript?
A nested loop in JavaScript is a loop placed inside another loop. It allows you to iterate over elements or perform repetitive tasks within a loop, which is part of another loop. This is often used for complex iterations or working with multi-dimensional data structures.
🌐
scaler.com
scaler.com › home › topics › nested loop in javascript with examples
Nested Loop in JavaScript with Examples - Scaler Topics
Why would I use nested loops?
Nested loops are used when you need to perform a repetitive task within a repetitive task. For example, you might use nested loops to iterate through rows and columns when working with multi-dimensional arrays. They are also useful for generating combinations, permutations, or comparing elements within multiple collections.
🌐
scaler.com
scaler.com › home › topics › nested loop in javascript with examples
Nested Loop in JavaScript with Examples - Scaler Topics
How do I control the number of iterations in nested loops?
You can control the number of iterations in nested loops by specifying the loop conditions for both the outer and inner loops. You can use loop variables (e.g., i and j in the example above) and set the limits based on your requirements.
🌐
scaler.com
scaler.com › home › topics › nested loop in javascript with examples
Nested Loop in JavaScript with Examples - Scaler Topics
🌐
EDUCBA
educba.com › home › software development › software development tutorials › javascript tutorial › nested loop in javascript
Nested Loop in JavaScript | Guide to Nested Loop - Flowchart & Examples
June 28, 2023 - JavaScript supports the nested loop feature, where a loop is present inside another loop. A loop can have one or number and/or n nested levels of loops defined inside another loop. For each outer loop, the inner loop gets to execute.
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Codedamn
codedamn.com › news › javascript
Nested for loops in JavaScript: Why?
January 4, 2023 - for(var i=4;i<6;i++){ for(var j=4;j<5;j++){ console.log(i,j); } }Code language: JavaScript (javascript) The above example is also available in a codedamn playground for you to modify and run. The output for the above example is as shown below. ... Below given is an image of the flowchart of how a nested for loop executes.
🌐
YouTube
youtube.com › daily tuition
Nested For Loop Important for Dynamic Array - JavaScript Tutorial Part - 12 - YouTube
Nested For Loop Important for Dynamic Array - JavaScript Tutorial Part - 12 In this tutorial, we will learn Nested for loop. Nested for loop use for dynamic ...
Published   October 9, 2018
Views   2K
🌐
Scaler
scaler.com › home › topics › nested loop in javascript with examples
Nested Loop in JavaScript with Examples - Scaler Topics
January 1, 2024 - You can use loop variables (e.g., i and j in the example above) and set the limits based on your requirements. JavaScript supports the concept of nested loops, where one loop is contained within another loop.
Top answer
1 of 7
21
var arr = [[1,2], [3,4], [5,6]];

This is an array of arrays. It is a little bit easier to read like this:

var arr = [
            [1,2],
            [3,4],
            [5,6]
          ];

That makes it a little bit easier to see that you have an array of 3 arrays. The outer 'for' will loop through each of 1st level arrays. So the very first outer for loop when i=0 you are going to grab the first inner array [1,2]:

for (var i=0; i < arr.length; i++) {
    //First time through i=0 so arr[i]=[1,2];
}

In the inner loop you are going to loop through each of the 3 inner arrays one at a time.

for (var j=0; j < arr[i].length; j++) {
    //Handle inner array.
}

This argument grabs the length of the inner array:

arr[i].length

So on your first time through the outer loop i=0 and arr[i] is going to equal [1,2] because you are grabbing the 0th element. Remember, arrays elements are always counted starting at 0, not 1.

Finally you are printing out the results with:

console.log(arr[i][j]);

The first time through you can break it down a little. i=0 and j=0. arr[0][0] which translates as grab the first element from the outer array and then the first element from the first inner array. In this case it is '1':

[
    [1,2], <-- 0
    [3,4], <-- 1
    [5,6]  <-- 2
];

The code will loop through the first first set [1,2], then the second [3,4], and so on.

2 of 7
9

The double for loop you have above works like so:

 var arr = [[1,2], [3,4], [5,6]];

 for (var i=0; i < arr.length; i++) {
  // i = 0, then we loop below:
  for (var j=0; j < arr[i].length; j++) {
    //here we loop through the array which is in the main array
    //in the first case, i = 0, j = 1, then we loop again, i = 0, j = 1
    console.log(arr[i][j]);
    //after we finish the stuff in the 'j' loop we go back to the 'i' loop 
    //and here i = 1, then we go down again, i, remains at 1, and j = 0, then j = 1
    //....rinse and repeat, 
  }
}

In plain english:

We grab the first element in the main array, which is an array itself, we loop through that, and log at each index, this is terminated by our length condition in the second loop. We then move to to the next index of the main array, which is an array itself.... and so on, until we reach the end of the main array

To access and index in the main array, we need to use array[i] - that index holds an array - so to go INTO that array, we need to use array[i][j]

Hope that makes sense!

Find elsewhere
🌐
Stack Overflow
stackoverflow.com › questions › 74308712 › can-you-use-dynamic-values-in-the-instructions-of-a-nested-loop-and-feedback-on
javascript - Can you use dynamic values in the instructions of a nested loop? and feedback on declaring variables (node.js) - Stack Overflow
<----------------------------------------------------------------------------------------------------------> Problem: 1. the nested loop only iterates i = 100 2. I think there is a problem lying within declaration of variables (string vs. integer) ... var input = ['100', '500', '12']; function decipher() { for (let i = input[0]; i <= input[1]; i++) { //loops executes correctly let sumMIN = 0; for (let j = 0; j < i.length; j++) { //loop stops at i = 100 sumMIN += parseInt(i[j]); console.log(sumMIN); if (sumMIN == input[2]) { console.log("true"); } else { console.log("false"); } } } } return decipher(input);
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › nesting-for-loops-in-javascript
Nesting For Loops in JavaScript - GeeksforGeeks
In JavaScript, you can nest different types of loops to achieve the desired iteration. Common types include a For Loop within another For Loop, which is a standard nested loop for iterating over multi-dimensional arrays.
Published   August 5, 2025
🌐
Softuni
js-book.softuni.org › chapter-06-nested-loops.html
6.1. Nested Loops · Programming Basics with JavaScript
Here is an example that illustrates nested loops. The aim is again to print a rectangle made of n *_ _n stars, in which for each row a loop iterates from 1 to n, and for each column a nested loop is executed from 1 to *n:
🌐
Medium
medium.com › analytics-vidhya › what-if-the-number-of-nested-loops-depends-on-inputs-at-runtime-4b161721eb22
What if the number of nested loops depends on inputs at runtime?
July 8, 2024 - ... Cartesian products of different ... but can be easily derived from the above discussion). Recursion is capable of rewriting a program's number of nested loops dynamically....
🌐
Log4JavaScript
log4javascript.org › home › js-framework › breaking down javascript nested for loops
JavaScript Nested For Loops: A Comprehensive Guide
July 4, 2023 - It has three parts: initialization, condition, and increment/decrement. Today, we delve deeper into a specialized form of the for loop – the nested for loop, which is essentially a for loop within another for loop.
🌐
Programiz PRO
programiz.pro › resources › js-nested-loop
Understanding Nested Loops in JavaScript
let height = 5; for (let i = height; i > 0; i--) { let row = ""; for (let j = 0; j < i; j++) { row += "* "; } console.log(row); } ... By following this step-by-step thinking process, you’ll be able to build any pattern using nested loops in JavaScript!
🌐
Delft Stack
delftstack.com › home › howto › javascript › nested for loops javascript
Nested for Loops JavaScript | Delft Stack
March 11, 2025 - At its core, a nested loop is a loop inside another loop. In JavaScript, you can use any type of loop (for, while, or do…while) as a nested loop. The outer loop runs first, and for each iteration of the outer loop, the inner loop runs completely.