Search notes:

Javascript: create all element pairs of an array

The following example demonstrates a method to produce an array that contains all combinations of pairs of an array.
function createAllPairs(list) {

   return list.reduce( (accu, item, i, arr) =>
   //
   // Iterattion over each element in list
   //
      accu.concat(
         arr.slice(i + 1).           // Create an array of the elements beyond the current element …
         map(_item => [item, _item]) // … and create a «pair» for each element.
      )
      ,
   // The following empty array will be passed as
   // initial value for accue to reduce() and
   // populated with pari-combinations in each
   // iteration:
      []
   );

}

let result = createAllPairs(['one', 'two', 'three', 'four', 'five']);

result.forEach(pair => print(`${pair[0]} - ${pair[1]}`));

Links

This Stackoverlow answer was quite helpful to me

Index