Kodeclik Blog


How to find all combinations of array values in Javascript

Let us suppose I have a list (or array) of grocery items such as ["apples", "bananas", "chips", "dates", "eggs"] and wish to create all pairwise combinations of items (but without generating duplicates). A simple Javascript program for this purpose would be:
This code uses two nested loops to iterate over the array and generate all possible pairs of items. The outer loop iterates over each item in the array, and the inner loop iterates over the remaining items in the array to generate all pairs that include the current item. The pairs are stored in an array called pairs, which is logged to the console at the end of the code.
The output of this code for the given items would be something like:
The reason you are getting [Array(2), Array(2), ....] when running the Javascript code is because the console is displaying the arrays as objects, which is the default behavior of console.log() for arrays. To display the contents of the arrays, you can use the console.table() method instead of console.log().
This will produce:
Note that our code generates all possible pairs of items without any duplicates, meaning that each pair is unique and does not appear in reverse order.
Alternatively, you can simply update the code to say:
Here we are doing console.log() on each element of “pairs”. The output will be:

Finding triples of values in Javascript

To get triples of values in Javascript, we extend the above code:
This code uses three nested loops to iterate over the array and generate all possible triples of items. The outer loop iterates over each item in the array, the second loop iterates over the remaining items in the array to generate all pairs that include the current item, and the innermost loop iterates over the further remaining items in the array to generate all triples that include the current pair. The triples are stored in an array called triples which looks like:
And thus the console.log() statement will print each element of this array (which is itself an array) on a separate line.

Finding all combinations of values in Javascript

The above gets cumbersome if we want to generate combinations of all sizes, not just a fixed size. To generate combinations of all sizes from the given array, we can use something like the following:
This code generates all possible combinations of sizes 1 to n from the given array, where n is the length of the array. The combinations are stored in an array called combinations, which is logged to the console at the end of the code.
The output of this code for the given items would be elements of this array:
As you can see, the code generates all possible combinations of sizes 1 to 5 from the given array, where each combination is unique and does not contain any duplicate items or the same items in different orders.

Finding all combinations of values in Javascript by recursion

In this approach, we explore a recursive strategy. The below code generates all possible combinations of the items in the items array by calling the generateCombinations function for each possible combination size, from 1 to the length of the items array. Note that the generateCombinations function uses recursion to generate all possible combinations of the given size. It does this by iterating through the arr array, starting at the start index, and adding each element to the temp array. If the temp array has reached the desired size, it adds a copy of the temp array to the combinations array. If the temp array has not reached the desired size, it calls itself recursively with the next index as the start index and the updated temp array. After all combinations have been generated, the combinations array is printed to the console using a forEach loop as usual.
The output is as before:
If you liked this blogpost, checkout our blogpost on list/array comprehensions in Javascript.
Want to learn Javascript with us? Sign up for 1:1 or small group classes.

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.
  • ABOUT

Copyright @ Kodeclik 2024. All rights reserved.