I'm facing a challenge with integrating JSON into existing objects using Vue.js. Currently, I have a function that calculates variants successfully, but I need to modify them to ensure accurate data is sent to my Laravel backend.
Here is what my array looks like (used for variant calculation):
"attributes":[
{
"title":{
"text":"Size",
"value":"1"
},
"values":[
"Red",
"Green"
]
},
{
"title":{
"text":"Color",
"value":"2"
},
"values":[
"X",
"M"
]
}
]
And this is how the calculation is done:
addVariant: function () {
const parts = this.form.attributes.map(attribute => attribute.values);
const combinations = parts.reduce((a, b) => a.reduce((r, v) => r.concat(b.map(w => [].concat(v, w))), []));
this.form.variants = combinations;
},
The current output:
"variants":[
[
"Red",
"X"
],
[
"Red",
"M"
],
[
"Green",
"X"
],
[
"Green",
"M"
]
]
However, I need the variants to be structured as follows:
"variants":[
{
"title": "Red/M",
"slug": "prooooo",
"options": [
{
"key": "Color",
"value": "Red"
},
{
"key": "Size",
"value": "M"
}
]
},
{
"title": "Red/X",
"slug": "prooooo",
"options": [
{
"key": "Color",
"value": "Red"
},
{
"key": "Size",
"value": "X"
}
]
}
The title field needs to be calculated based on options.values. How can I transform the JSON structure to match the desired format?
Any suggestions on accomplishing this? Thanks in advance!