'How to achieve this json in POST request react?
I have this POST request:
await fetch('http://localhost:8080/api/order', {
method: 'POST',
body: JSON.stringify({
orderNumber: 1,
customer: {customerId:1},
orderDetails: cartCtx.items,
pmethod: "karta",
}
but can you achieve JSON like this? The problem is with orderDetails because i dont know how to group my cartCtx.items array like in the picture. Any ideas?
This is my array.
Solution 1:[1]
basically my orderDetails is an array and inside this array i want to have an additional group of foodItem for my foodItemId but quantity have to be outside this additional field. If i do something like this :
orderDetails: {foodItem:cartCtx.items}both id and quantity are inside this foodItem
You are on the right track. The orderDetails list needs a list of objects which each have a "foodItem" key. One way create an array of objects with this key is to use map():
cartCtx.items.map(item => {foodItem: item})
This creates a new array with the required key and each of the original items nested inside.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
