[英]Using map function to reformat array of objects
我需要一些帮助来重新格式化对象的结构。
我从 API 获取一组对象,然后我需要那些专门格式化的对象。
这是我收到的数组中两个对象的示例。 在此示例中,它们是相同的。
[
{
"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
}
]
但是,我只需要将这些对象中的某些键打印在另一个对象数组中。 像这样:
[
//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: ''
}
]
带有空字符串的键稍后可能会或可能不会填充数据,但它们需要在最终对象结果中。
原始数组中的每个对象都有一个父对象和随机数量的子对象。 我需要以某种方式循环遍历数组中的每个对象并按顺序打印出父项和子项。
感谢您提前提供任何建议或帮助。
你可以使用 reduce 来创建这个结构:
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);
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.