I have received some JSON data that has a specific structure:
[
{
"items": [
{
"id": "xxxx",
"description": {
"style": "",
"specs": ""
}
},
{
"id": "xxxx",
"description": {
"style": "",
"specs": ""
}
}
],
"category": "xxxxxx",
"name": "xxxxxx"
},
{
"items": [
{
"id": "xxxx",
"description": {
"style": "",
"specs": ""
}
},
{
"id": "xxxx",
"description": {
"style": "",
"specs": ""
}
}
],
"category": "xxxxxx",
"name": "xxxxxx"
}
]
This JSON is stored in the variable 'newItem' and it is being processed in two different parts of the code I'm currently working on:
that.cart.addItem(newItem.toObject());
And later:
that.origCart.replaceWith(that.cart);
In the past, this process worked well when 'items' was flat. However, with our new JSON structure, I now need to handle 'items' within each category/name separately.
When iterating through a loop, I am able to access the individual items using:
newItem.items
But when trying to add these items to the cart, I encounter a Type Error. (I will eventually have multiple 'carts' once this issue is resolved.)
that.cart.addItem(newItem.items.toObject());
TypeError: newItem.items.toObject is not a function
Can anyone provide guidance on how to properly handle 'newItem.items' or suggest an alternative approach to setting them correctly? I have tried various solutions found online with no success, so I am reaching out to experts for assistance as this area is not my strong suit. Thank you.