JSFiddle - React, Tailwind, and code Playground
by Shedal
JavaScript
var options = {
optionOne: [true, false],
optionTwo: [true, false],
optionThree: [
null,
{ property1: 9, property2: 7 },
{ property1: 4, property2: 12 },
{ property1: 16, property2: 14 }
]
};
function getCombinations(options, optionIndex, results, current) {
var allKeys = Object.keys(options);
var optionKey = allKeys[optionIndex];
var vals = options[optionKey];
for (var i = 0; i < vals.length; i++) {
current[optionKey] = vals[i];
if (optionIndex + 1 < allKeys.length) {
getCombinations(options, optionIndex + 1, results, current);
} else {
// The easiest way to clone an object.
var res = JSON.parse(JSON.stringify(current));
results.push(res);
}
}
return results;
}
var results = getCombinations(options, 0, [], {});
document.body.innerHTML = JSON.stringify(results);