I'm having difficulty with a JavaScript algorithm. I am working with Vue as my framework and state management tool. I have an object that needs to be 'calculated' as variants.
For instance, I have 3 objects with some values as arrays, like this:
"attributes":[
{
"title":{
"text":"Size",
"value":"1"
},
"values":[
"M",
"L",
"X"
]
},
{
"title":{
"text":"Color",
"value":"2"
},
"values":[
"Red",
"Green",
"Black"
]
},
{
"title":{
"text":"Material"
},
"values":[
"Cotton",
]
}
]
Therefore, the state for this is obviously attributes. What I need to accomplish is to iterate through each attribute value and create a variant for every possible combination, then log them to the console. For example:
M/Red/Cotton, L/Red/Cotton, X/Red/Cotton, M/Green/Cotton, L/Green/Cotton, X/Green/Cotton, M/Black/Cotton, L/Black/Cotton, X/Black/Cotton
I've started a function that only iterates through attributes, so now I need to iterate through values and generate those codes mentioned above, but I'm not sure how to proceed. This is what I have so far:
addVariant: function (text) {
this.attributes.forEach(element => console.log(element));
},