Start a new topic

How do I generate the cartesian product of object properties?

//This merge gate code will take in an object x0 and generate all possible combinations of its properties.

//This is useful in breaking a list into a series of options for the data driver exoskeleton.

//This technique is useful when using a data driver exoskeleton to run a service call many times over again for all possible combinations of input.

//e.g., user selects [term:["cancer","HIV","therapy"], term2:["penicillin", "THC", "Alcohol"]] and a list of all possible combinations is created e.g.,


function() { function product(args) { if(!args.length) return [[]]; var prod = product(args.slice(1)), r = []; args[0].forEach(function(x) { prod.forEach(function(p) { r.push([x].concat(p)); }); }); return r; } function objectProduct(obj) { var keys = Object.keys(obj), values = { return obj[x] }); return product(values).map(function(p) { var e = {}; keys.forEach(function(k, n) { e[k] = p[n] }); return e; }); }return(objectProduct(x0)) }()

Login to post a comment