I need some help reformatting the structure of an object.
I am getting an array of objects from an API and I then need those specifically formatted.
Here is an example of two objects in the array that I receive. In this example, they are identical.
[
{
"parent": {
"id": 0,
"recnum": 5030000000,
"prtnme": "Wheel Assembly with Bearings",
"location": null,
"controlType": 0,
"warehouse": null,
"stkitm": false,
"qtyohn": -271,
"prtcst": 0,
"avgcst": 0,
"printed": 0,
"preexisting": false
},
"children": [
{
"id": 1,
"recnum": 5030000000,
"prtnme": "Wheel Assembly with Bearings",
"location": {
"id": 2,
"number": "101",
"description": "Lot 1",
"active": true,
"descriptionScanner": "Lot 1"
},
"controlType": 1,
"warehouse": {
"id": 1,
"number": 100,
"description": "TechnoGuard 21456 Atlantic Blvd-1",
"nettable": true,
"active": true
},
"stkitm": true,
"qtyohn": -201,
"prtcst": 0,
"avgcst": 0,
"printed": 0,
"preexisting": false
},
{
"id": 3,
"recnum": 5030000000,
"prtnme": "Wheel Assembly with Bearings",
"location": {
"id": 4,
"number": "201",
"description": "Serial 1",
"active": true,
"descriptionScanner": "Serial 1"
},
"controlType": 2,
"warehouse": {
"id": 1,
"number": 100,
"description": "TechnoGuard 21456 Atlantic Blvd-1",
"nettable": true,
"active": true
},
"stkitm": true,
"qtyohn": -70,
"prtcst": 0,
"avgcst": 0,
"printed": 0,
"preexisting": false
}
],
"nulled": null
},
{
"parent": {
"id": 0,
"recnum": 5030000000,
"prtnme": "Wheel Assembly with Bearings",
"location": null,
"controlType": 0,
"warehouse": null,
"stkitm": false,
"qtyohn": -271,
"prtcst": 0,
"avgcst": 0,
"printed": 0,
"preexisting": false
},
"children": [
{
"id": 1,
"recnum": 5030000000,
"prtnme": "Wheel Assembly with Bearings",
"location": {
"id": 2,
"number": "101",
"description": "Lot 1",
"active": true,
"descriptionScanner": "Lot 1"
},
"controlType": 1,
"warehouse": {
"id": 1,
"number": 100,
"description": "TechnoGuard 21456 Atlantic Blvd-1",
"nettable": true,
"active": true
},
"stkitm": true,
"qtyohn": -201,
"prtcst": 0,
"avgcst": 0,
"printed": 0,
"preexisting": false
},
{
"id": 3,
"recnum": 5030000000,
"prtnme": "Wheel Assembly with Bearings",
"location": {
"id": 4,
"number": "201",
"description": "Serial 1",
"active": true,
"descriptionScanner": "Serial 1"
},
"controlType": 2,
"warehouse": {
"id": 1,
"number": 100,
"description": "TechnoGuard 21456 Atlantic Blvd-1",
"nettable": true,
"active": true
},
"stkitm": true,
"qtyohn": -70,
"prtcst": 0,
"avgcst": 0,
"printed": 0,
"preexisting": false
}
],
"nulled": null
}
]
However, I only need certain keys from these objects printed out in another array of objects. like this:
[
//PARENT
{
recNum: parent.recnum,
prtNme: parent.prtnme,
date: '',
locNum: '',
locDescription: '',
lotSerNum: '',
byTrans: '',
running: parent.qtyohn,
unitCost: '',
extCost: ''
},
//CHILDREN
{
recNum: '',
prtNme: '',
date: '',
locNum: children[].location.number,
locDescription: children[].location.description,
lotSerNum: '',
byTrans: '',
running: children[].qtyohn,
unitCost: '',
extCost: ''
}
]
The keys with empty strings may or may not be filled in with data later, but they need to be in the end object result.
Each object in the original array has one parent and a random amount of children. I need to somehow loop through each object in the array and print out the parent and the children in order.
Thanks for any suggestions or help in advance.
CodePudding user response:
you can use reduce to create this structure:
const apiData = [
{
"parent": {
"id": 0,
"recnum": 5030000000,
"prtnme": "Wheel Assembly with Bearings",
"location": null,
"controlType": 0,
"warehouse": null,
"stkitm": false,
"qtyohn": -271,
"prtcst": 0,
"avgcst": 0,
"printed": 0,
"preexisting": false
},
"children": [
{
"id": 1,
"recnum": 5030000000,
"prtnme": "Wheel Assembly with Bearings",
"location": {
"id": 2,
"number": "101",
"description": "Lot 1",
"active": true,
"descriptionScanner": "Lot 1"
},
"controlType": 1,
"warehouse": {
"id": 1,
"number": 100,
"description": "TechnoGuard 21456 Atlantic Blvd-1",
"nettable": true,
"active": true
},
"stkitm": true,
"qtyohn": -201,
"prtcst": 0,
"avgcst": 0,
"printed": 0,
"preexisting": false
},
{
"id": 3,
"recnum": 5030000000,
"prtnme": "Wheel Assembly with Bearings",
"location": {
"id": 4,
"number": "201",
"description": "Serial 1",
"active": true,
"descriptionScanner": "Serial 1"
},
"controlType": 2,
"warehouse": {
"id": 1,
"number": 100,
"description": "TechnoGuard 21456 Atlantic Blvd-1",
"nettable": true,
"active": true
},
"stkitm": true,
"qtyohn": -70,
"prtcst": 0,
"avgcst": 0,
"printed": 0,
"preexisting": false
}
],
"nulled": null
},
{
"parent": {
"id": 0,
"recnum": 5030000000,
"prtnme": "Wheel Assembly with Bearings",
"location": null,
"controlType": 0,
"warehouse": null,
"stkitm": false,
"qtyohn": -271,
"prtcst": 0,
"avgcst": 0,
"printed": 0,
"preexisting": false
},
"children": [
{
"id": 1,
"recnum": 5030000000,
"prtnme": "Wheel Assembly with Bearings",
"location": {
"id": 2,
"number": "101",
"description": "Lot 1",
"active": true,
"descriptionScanner": "Lot 1"
},
"controlType": 1,
"warehouse": {
"id": 1,
"number": 100,
"description": "TechnoGuard 21456 Atlantic Blvd-1",
"nettable": true,
"active": true
},
"stkitm": true,
"qtyohn": -201,
"prtcst": 0,
"avgcst": 0,
"printed": 0,
"preexisting": false
},
{
"id": 3,
"recnum": 5030000000,
"prtnme": "Wheel Assembly with Bearings",
"location": {
"id": 4,
"number": "201",
"description": "Serial 1",
"active": true,
"descriptionScanner": "Serial 1"
},
"controlType": 2,
"warehouse": {
"id": 1,
"number": 100,
"description": "TechnoGuard 21456 Atlantic Blvd-1",
"nettable": true,
"active": true
},
"stkitm": true,
"qtyohn": -70,
"prtcst": 0,
"avgcst": 0,
"printed": 0,
"preexisting": false
}
],
"nulled": null
}
];
const flatApiData = apiData.reduce((list, obj) => [...list, obj.parent, ...obj.children], []);
console.log(flatApiData);
CodePudding user response:
const data = [
{
"parent": {
"id": 0,
"recnum": 5030000000,
"prtnme": "Wheel Assembly with Bearings",
"location": null,
"controlType": 0,
"warehouse": null,
"stkitm": false,
"qtyohn": -271,
"prtcst": 0,
"avgcst": 0,
"printed": 0,
"preexisting": false
},
"children": [
{
"id": 1,
"recnum": 5030000000,
"prtnme": "Wheel Assembly with Bearings",
"location": {
"id": 2,
"number": "101",
"description": "Lot 1",
"active": true,
"descriptionScanner": "Lot 1"
},
"controlType": 1,
"warehouse": {
"id": 1,
"number": 100,
"description": "TechnoGuard 21456 Atlantic Blvd-1",
"nettable": true,
"active": true
},
"stkitm": true,
"qtyohn": -201,
"prtcst": 0,
"avgcst": 0,
"printed": 0,
"preexisting": false
},
{
"id": 3,
"recnum": 5030000000,
"prtnme": "Wheel Assembly with Bearings",
"location": {
"id": 4,
"number": "201",
"description": "Serial 1",
"active": true,
"descriptionScanner": "Serial 1"
},
"controlType": 2,
"warehouse": {
"id": 1,
"number": 100,
"description": "TechnoGuard 21456 Atlantic Blvd-1",
"nettable": true,
"active": true
},
"stkitm": true,
"qtyohn": -70,
"prtcst": 0,
"avgcst": 0,
"printed": 0,
"preexisting": false
}
],
"nulled": null
},
{
"parent": {
"id": 0,
"recnum": 5030000000,
"prtnme": "Wheel Assembly with Bearings",
"location": null,
"controlType": 0,
"warehouse": null,
"stkitm": false,
"qtyohn": -271,
"prtcst": 0,
"avgcst": 0,
"printed": 0,
"preexisting": false
},
"children": [
{
"id": 1,
"recnum": 5030000000,
"prtnme": "Wheel Assembly with Bearings",
"location": {
"id": 2,
"number": "101",
"description": "Lot 1",
"active": true,
"descriptionScanner": "Lot 1"
},
"controlType": 1,
"warehouse": {
"id": 1,
"number": 100,
"description": "TechnoGuard 21456 Atlantic Blvd-1",
"nettable": true,
"active": true
},
"stkitm": true,
"qtyohn": -201,
"prtcst": 0,
"avgcst": 0,
"printed": 0,
"preexisting": false
},
{
"id": 3,
"recnum": 5030000000,
"prtnme": "Wheel Assembly with Bearings",
"location": {
"id": 4,
"number": "201",
"description": "Serial 1",
"active": true,
"descriptionScanner": "Serial 1"
},
"controlType": 2,
"warehouse": {
"id": 1,
"number": 100,
"description": "TechnoGuard 21456 Atlantic Blvd-1",
"nettable": true,
"active": true
},
"stkitm": true,
"qtyohn": -70,
"prtcst": 0,
"avgcst": 0,
"printed": 0,
"preexisting": false
}
],
"nulled": null
}
];
const newData = [];
data.forEach(
(ele)=>{
const {recnum,prtnme,qtyohn} = ele.parent;
const {children} = ele;
newData.push(
{
recNum: recnum,
prtNme: prtnme,
date: '',
locNum: '',
locDescription: '',
lotSerNum: '',
byTrans: '',
running: qtyohn,
unitCost: '',
extCost: ''
}
);
children.forEach(
(cele)=>{
newData.push(
{
recNum: '',
prtNme: '',
date: '',
locNum: cele.location.number,
locDescription: cele.location.description,
lotSerNum: '',
byTrans: '',
running: cele.qtyohn,
unitCost: '',
extCost: ''
});
}
);
}
);
console.log(newData);