Answer the question
In order to leave comments, you need to log in
How to combine array and object in json?
At the moment there is an array cart, inside of which there are objects. The array needs to be converted to a json object, but after performing the operation, an object is created with a single key, in which the last element of the array is written
var cart = [];
var orderDetail1 = {
dishId: 1,
price: 200,
quantyty: 1,
}
var orderDetail2 = {
dishId: 2,
price: 150,
quantyty: 4,
}
var order = {
tableId: 1,
shiftId: 5,
finalPrice: 800
}
cart.push(orderDetail1 );
cart.push(orderDetail2 );
{
"order" : {
"tableId" : 1 ,
"shiftId" : 5,
"finalPrice": 800
},
"orderDetails" : [
{
"dishId" : 1,
"price" : 200,
"quantyty": 1
},
{
"dishId" : 2,
"price" : 150,
"quantyty": 4
},
]
}
Answer the question
In order to leave comments, you need to log in
var json = JSON.stringify({
order: order,
orderDetails: cart
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question