Cartesian product of objects in javascript

落爺英雄遲暮 提交于 2019-11-28 09:27:13

Once you get rid of the ' 'i' is a global var issue', you can get to the result with this code for instance :

var input = [
    { 'colour' : ['red', 'green'] },
    { 'material' : ['cotton', 'wool', 'silk'] },
    { 'shape' : ['round', 'square', 'rectangle'] }
];

function cartesianProduct(input, current) {
   if (!input || !input.length) { return []; }

   var head = input[0];
   var tail = input.slice(1);
   var output = [];

    for (var key in head) {
      for (var i = 0; i < head[key].length; i++) {
            var newCurrent = copy(current);         
            newCurrent[key] = head[key][i];
            if (tail.length) {
                 var productOfTail = 
                         cartesianProduct(tail, newCurrent);
                 output = output.concat(productOfTail);
            } else output.push(newCurrent);
       }
     }    
    return output;
}

function copy(obj) {
  var res = {};
  for (var p in obj) res[p] = obj[p];
  return res;
}


console.log(cartesianProduct(input));

Heres a solution using Ramda.js

const input = [
  {
    'colour': ['red', 'green']
  },
  {
    'material': ['cotton', 'wool', 'silk']
  },
  {
    'shape': ['round', 'square', 'rectangle']
  }
]

const cartesianProductList = (Xs) => (
  R.reduce(
    (Ys, X) => (
      R.map(R.apply(R.append), R.xprod(X, Ys))
    ), 
    [[]],
    Xs
  )
)

const xPairs = (x, xs) => (
  R.map(R.pair(x), xs)
)

const cartesianProductObject = (objs) => (
  R.pipe(
    R.mergeAll,
    R.toPairs,
    R.map(R.apply(xPairs)),
    cartesianProductList,
    R.map(R.fromPairs),
  )(objs)
)

console.log(cartesianProductObject(input))
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!