I have to make an array of all combinations with attribute values.
Attributes/values object:
let attributes = {
color: ['red', 'green', 'blue'],
sizes: ['sm', 'md', 'lg'],
material: ['cotton', 'wool']
}
I need to make an array of all possible combinations as such.
color sizes material
red sm cotton
red sm wool
red md cotton
red md wool
red lg cotton
red lg wool
blue sm cotton
blue sm wool
blue md cotton
blue md wool
blue lg cotton
blue lg wool
green sm cotton
green sm wool
green md cotton
green md wool
green lg cotton
green lg wool
The attribute types and values count are both indefinite (at least 1). How can I achieve this?
This is the code I have so far
// keys = ['color', 'sizes', 'material']
// attributes is the same as above.
for (let keysIndex = 0; keysIndex < keys.length; keysIndex++) {
let key = keys[i];
let values = attributes[key];
//loop through each value
for (let valuesIndex = 0; valuesIndex < values.length; valuesIndex++) {
// for each value, loop through all keys
for (let keysIndex2 = 0; keysIndex2 < keys.length; keysIndex2++) {
if (keysIndex === keysIndex2) {
continue;
}
for (let valuesIndex2 = 0; valuesIndex2 < values.length; valuesIndex2++) {
// What do I do from here?
// Not sure if this is even the right path?
}
}
}
}