I have the following response structure [{{},[{},{},{}]}, {{},[{},{},{}]}, {{},[{},{},{}]}]
, expected structure [{}, {}, {}]
.
Example response, would like to make it a separate objects with in the array (please check the desired result below).
{
"itemDetails":[
{
"itemInfo":{
"id":"a1",
"firstname":"watch"
},
"itemOrders":[
{
"orderNo":"xyz1",
"orderId":"abc1"
},
{
"orderNo":"xyz2",
"orderId":"abc2"
},
{
"orderNo":"xyz3",
"orderId":"abc3"
}
]
},
{
"itemInfo":{
"id":"a2",
"firstname":"mobile"
},
"itemOrders":[
{
"orderNo":"xyz4",
"orderId":"abc4"
},
{
"orderNo":"xyz5",
"orderId":"abc5"
},
{
"orderNo":"xyz6",
"orderId":"abc6"
}
]
}
]
}
Desired result: able to access the itemDetails
in my code, but I need itemDetails
in the below format.
{
itemDetails: [
{
"id": "a1",
"firstname": "watch"
},
{
"orderNo":"xyz1",
"orderId":"abc1"
},
{
"orderNo":"xyz2",
"orderId":"abc2"
},
{
"orderNo":"xyz3",
"orderId":"abc3"
},
{
"id": "a2",
"firstname": "mobile"
}
{
"orderNo":"xyz4",
"orderId":"abc4"
},
{
"orderNo":"xyz5",
"orderId":"abc5"
},
{
"orderNo":"xyz6",
"orderId":"abc6"
}
]
}
CodePudding user response:
Maybe this can help:
const transformItemDetails = (itemsDetails) => {
const newArr = [];
itemsDetails.forEach((item) => {
newArr.push({ ...item.itemInfo });
item.itemOrders.forEach((order) => newArr.push(order));
});
return newArr;
};
const obj = {
itemDetails: [
{
itemInfo: {
id: "a1",
firstname: "watch",
},
itemOrders: [
{
orderNo: "xyz1",
orderId: "abc1",
},
{
orderNo: "xyz2",
orderId: "abc2",
},
{
orderNo: "xyz3",
orderId: "abc3",
},
],
},
{
itemInfo: {
id: "a2",
firstname: "mobile",
},
itemOrders: [
{
orderNo: "xyz4",
orderId: "abc4",
},
{
orderNo: "xyz5",
orderId: "abc5",
},
{
orderNo: "xyz6",
orderId: "abc6",
},
],
},
],
};
console.log(transformItemDetails(obj.itemDetails));
CodePudding user response:
This is just basic object and array manipulation inside nested for loops. You start with an object that contains an array. The elements of this array are objects. Inside each of these is are object and an array. It can quickly get confusing so takes a little practice.
let originalObject ={
"itemDetails":[
{
"itemInfo":{
"id":"a1",
"firstname":"watch"
},
"itemOrders":[
{
"orderNo":"xyz1",
"orderId":"abc1"
},
{
"orderNo":"xyz2",
"orderId":"abc2"
},
{
"orderNo":"xyz3",
"orderId":"abc3"
}
]
},
{
"itemInfo":{
"id":"a2",
"firstname":"mobile"
},
"itemOrders":[
{
"orderNo":"xyz4",
"orderId":"abc4"
},
{
"orderNo":"xyz5",
"orderId":"abc5"
},
{
"orderNo":"xyz6",
"orderId":"abc6"
}
]
}
]
}
let originalArray = originalObject['itemDetails']
let desiredOutput = {};
desiredOutput.itemDetails =[]
let newArray = [];
for (let i = 0; i < originalArray.length; i ){
let newObject = {}
newObject.id = originalArray[i]['itemInfo']['id']
newObject.firstName = originalArray[i]['itemInfo']['firstname']
desiredOutput.itemDetails.push(newObject)
let itemOrders = originalArray[i]['itemOrders']
for (let j = 0; j < itemOrders.length; j ){
let anotherObject = {}
anotherObject.orderNo = itemOrders[j]['orderNo']
anotherObject.orderId = itemOrders[j]['orderId']
desiredOutput.itemDetails.push(anotherObject)
}
}
console.log(desiredOutput)
/*
let desiredOutput = {
itemDetails: [
{
"id": "a1",
"firstname": "watch"
},
{
"orderNo":"xyz1",
"orderId":"abc1"
},
{
"orderNo":"xyz2",
"orderId":"abc2"
},
{
"orderNo":"xyz3",
"orderId":"abc3"
},
{
"id": "a2",
"firstname": "mobile"
},
{
"orderNo":"xyz4",
"orderId":"abc4"
},
{
"orderNo":"xyz5",
"orderId":"abc5"
},
{
"orderNo":"xyz6",
"orderId":"abc6"
}
]
}
*/