Javascript - Generating all combinations of elements in a single array (in pairs)
Here are some functional programming solutions:
Using EcmaScript2019's flatMap
:
var array = ["apple", "banana", "lemon", "mango"];var result = array.flatMap( (v, i) => array.slice(i+1).map( w => v + ' ' + w ));console.log(result);
Before the introduction of flatMap
(my answer in 2017), you would go for reduce
or [].concat(...)
in order to flatten the array:
var array = ["apple", "banana", "lemon", "mango"];var result = array.reduce( (acc, v, i) => acc.concat(array.slice(i+1).map( w => v + ' ' + w )),[]);console.log(result);
Or:
var array = ["apple", "banana", "lemon", "mango"];var result = [].concat(...array.map( (v, i) => array.slice(i+1).map( w => v + ' ' + w )));console.log(result);
A simple way would be to do a double for loop over the array where you skip the first i
elements in the second loop.
let array = ["apple", "banana", "lemon", "mango"];let results = [];// Since you only want pairs, there's no reason// to iterate over the last element directlyfor (let i = 0; i < array.length - 1; i++) { // This is where you'll capture that last value for (let j = i + 1; j < array.length; j++) { results.push(`${array[i]} ${array[j]}`); }}console.log(results);
Rewritten with ES5:
var array = ["apple", "banana", "lemon", "mango"];var results = [];// Since you only want pairs, there's no reason// to iterate over the last element directlyfor (var i = 0; i < array.length - 1; i++) { // This is where you'll capture that last value for (var j = i + 1; j < array.length; j++) { results.push(array[i] + ' ' + array[j]); }}console.log(results);
Although solutions have been found, I post here an algorithm for general case to find all combinations size n
of m (m>n)
elements. In your case, we have n=2
and m=4
.
const result = [];result.length = 2; //n=2function combine(input, len, start) { if(len === 0) { console.log( result.join(" ") ); //process here the result return; } for (let i = start; i <= input.length - len; i++) { result[result.length - len] = input[i]; combine(input, len-1, i+1 ); }}const array = ["apple", "banana", "lemon", "mango"]; combine( array, result.length, 0);