Dash separated cartesian product of any number of arrays in JavaScript

JavascriptWeb DevelopmentFront End TechnologyObject Oriented Programming

We are required to write a JavaScript function that takes in any number of arrays of literals. The function should compute and return an array of cartesian product of all the elements from the arrays separating them with a dash('−').


The code for this will be −

const arr1= [ 'a', 'b', 'c', 'd' ];
const arr2= [ '1', '2', '3' ];
const arr3= [ 'x', 'y', ];
const dotCartesian = (...arrs) => {
   const res = arrs.reduce((acc, val) => {
      let ret = [];
      acc.map(obj => {
         val.map(obj_1 => {
            ret.push(obj + '−' + obj_1)
      return ret;
   return res;
console.log(dotCartesian(arr1, arr2, arr3));


And the output in the console will be −

   'a−1−x', 'a−1−y', 'a−2−x',
   'a−2−y', 'a−3−x', 'a−3−y',
   'b−1−x', 'b−1−y', 'b−2−x',
   'b−2−y', 'b−3−x', 'b−3−y',
   'c−1−x', 'c−1−y', 'c−2−x',
   'c−2−y', 'c−3−x', 'c−3−y',
   'd−1−x', 'd−1−y', 'd−2−x',
   'd−2−y', 'd−3−x', 'd−3−y'
Updated on 25-Nov-2020 11:35:37