Javascript - Generating all combinations of elements in a single array (in pairs) Javascript - Generating all combinations of elements in a single array (in pairs) arrays arrays

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);