Home / Frontend Codes and Demos / JavaScript / Javascript Permutations and Combinations of Array Values

In this tutorial, The JavaScript functions to calculate combinations of elements in Array. This JavaScript algorithm will find all combinations of JavaScript array values. This algorithm is one of the most efficient way to get permutations and combinations of Array values.

Javascript Permutations and Combinations of Array Values

In this tutorial, The JavaScript functions to calculate combinations of elements in Array. This JavaScript algorithm will find all combinations of JavaScript array values. This algorithm is one of the most efficient way to get permutations and combinations of Array values.

{loadposition position-9}

Find the permutations and combinations JavaScript algorithm

function getPermutation(array, prefix) {   prefix = prefix || '';   if (!array.length) {   	return prefix;   }    var result = array[0].reduce(function (result, value) {   	return result.concat(getPermutation(array.slice(1), prefix + value));   }, []);   return result; }

Input

['a','b','c'], ['12','45'],['67','89']

OutPut

 a1267 a1289 a4567 a4589 b1267 b1289 b4567 b4589 c1267 c1289 c4567 c4589 

The reduce() method applies a function against an accumulator and each value of the array (from left-to-right) to reduce it to a single value.

Demo

About CV

I'm frontend developer

Check Also

Develop html table using css div tags

Develop html table using css div tags

This tutorial will explain how to form HTML table using css div tags without using table tags. This tutorial supports fixed header on scrolling table body and support responsive table.