This is a computed property in Vue.js:
shopCart() {
var scart = [],
group,
node;
scart.push({
payMethod: 0,
transactionReference: "",
RoomNumber: 0,
addressId: 0,
deliveryMinutes: 0,
comment: "",
posList: [],
});
for (var x = 0; x < this.items.length; x++) {
group = this.items[x].selectedOptions;
scart[0].posList.push({
articleId: this.items[x].id,
quantity: this.items[x].quantity,
singlePrice: this.items[x].price,
variantList: [],
parent: true,
name: this.items[x].name,
});
for (var y = 0; y < group.length; y++) {
node = group[y].options;
for (var z = 0; z < node.length; z++) {
scart[0].posList[0].variantList += node[z].id + ",";
}
}
}
scart = scart.map(function (e) {
return JSON.stringify(e);
});
scart = scart.join(",");
return scart;
},
It generates the following object:
{
"payMethod": 0,
"transactionReference": "",
"RoomNumber": 0,
"addressId": 0,
"deliveryMinutes": 0,
"comment": "",
"posList": [
{
"articleId": 20001,
"quantity": 1,
"singlePrice": 8.99,
"variantList": "2001,4001,5001,2003,4003,",
"parent": true,
"name": "Fisch Filet"
},
{
"articleId": 20002,
"quantity": 1,
"singlePrice": 8.99,
"variantList": "",
"parent": true,
"name": "Cheese Burger"
}
]
}
I am looking to achieve this format:
{
"payMethod": 0,
"transactionReference": "",
"RoomNumber": 0,
"addressId": 0,
"deliveryMinutes": 0,
"comment": "",
"posList": [
{
"articleId": 20001,
"quantity": 1,
"singlePrice": 8.99,
"variantList": "2001,4001,5001,",
"parent": true,
"name": "Fisch Filet"
},
{
"articleId": 20002,
"quantity": 1,
"singlePrice": 8.99,
"variantList": "2003,4003,",
"parent": true,
"name": "Cheese Burger"
}
]
}
The issue lies with the nested loop writing the string only into the first occurrence of "variantList". I need help figuring out how to handle the occurrence of "variantList' in the previous loop correctly. Any guidance would be greatly appreciated.
Items.json:
[
{
"id": 20001,
"name": "Fisch Filet",
"quantity": 1,
"selectedOptions": [
{
"id": 1,
"name": "Beilage",
"options": [
{
"id": 2001,
"optionQuantity": 1
}
]
},
{
"id": 2,
"name": "Salate",
"options": [
{
"id": 4001,
"optionQuantity": 1
}
]
},
{
"id": 3,
"name": "Ketchup/Mayo",
"options": [
{
"id": 5001,
"optionQuantity": 1
}
]
}
]
},
{
"id": 20002,
"name": "Cheese Burger",
"quantity": 1,
"selectedOptions": [
{
"id": 1,
"name": "Beilage",
"options": [
{
"id": 2003,
"optionQuantity": 1
}
]
},
{
"id": 2,
"name": "Salate",
"options": [
{
"id": 4003,
"optionQuantity": 1
}
]
}
]
}
]