JSFiddle - React, Tailwind, and code Playground
Distribute Object Properties
by skibulk
HTML
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
JavaScript
function distributeProperties(obj) {
var key, val, copies, i, j, copy, out = [{}];
// Iterate Properties
for (key in obj) {
val = obj[key];
// Check if Property is Array
if (val instanceof Array) {
copies = [];
// Handle Empty Arrays
if (val.length == 0) {
val.push([]);
}
// Iterate Property
for (j = 0; j < val.length; j++) {
// Iterate Output
for (i = 0; i < out.length; i++) {
// Clone Output
copy = $.extend(true, {}, out[i]);
copy[key] = val[j];
copies.push(copy);
}
}
out = copies;
} else {
// Iterate Output
for (i = 0; i < out.length; i++) {
out[i][key] = val;
}
}
}
return out;
}
var sims = distributeProperties({
log: [],
buyCue: [0, 1, 2],
sellCue: [0, 1, 2]
});
sims[0].log.push("Buy");
console.log(sims);