在 API 的父对象中合并子对象
Posted
技术标签:
【中文标题】在 API 的父对象中合并子对象【英文标题】:merge child object in parent object in API 【发布时间】:2021-01-17 00:59:42 【问题描述】:我必须将所有数据合并到一个对象中。我有一个对象数组,每个对象都有一个嵌入对象,例如:
[
robots: [],
lines: [],
_id: 5f753118391fe067dcfa1c88,
shift: 'Shift A',
date: 2020-10-01T00:00:00.000Z,
rawData:
robot1_runTime: 4989,
robot2_runTime: 6956,
robot2_waitTime: 6944,
,
createdAt: 2020-10-01T01:30:00.918Z,
updatedAt: 2020-10-01T02:47:22.107Z,
__v: 0
,
robots: [],
lines: [],
_id: 5f753118391fe067dcfa1c88,
shift: 'Shift A',
date: 2020-10-01T00:00:00.000Z,
rawData:
robot1_runTime: 4989,
robot2_runTime: 6956,
robot2_waitTime: 6944,
,
createdAt: 2020-10-01T01:30:00.918Z,
updatedAt: 2020-10-01T02:47:22.107Z,
__v: 0
]
我必须将嵌入对象 rawData
中的字段合并到父对象中,如下所示:
[
robots: [],
lines: [],
_id: 5f753118391fe067dcfa1c88,
shift: 'Shift A',
date: 2020-10-01T00:00:00.000Z,
robot1_runTime: 4989,
robot2_runTime: 6956,
robot2_waitTime: 6944,
createdAt: 2020-10-01T01:30:00.918Z,
updatedAt: 2020-10-01T02:47:22.107Z,
__v: 0
,
robots: [],
lines: [],
_id: 5f753118391fe067dcfa1c88,
shift: 'Shift A',
date: 2020-10-01T00:00:00.000Z,
robot1_runTime: 4989,
robot2_runTime: 6956,
robot2_waitTime: 6944,
createdAt: 2020-10-01T01:30:00.918Z,
updatedAt: 2020-10-01T02:47:22.107Z,
__v: 0
]
rawData
嵌入对象中的每个字段都应该是外部对象中的一个字段,完全消除了嵌入对象。
谁能告诉我如何实现它,以便我可以将 API 提供给另一个团队?
【问题讨论】:
【参考方案1】:这是一个关于如何将此子属性合并到父对象的示例。
let obj =
robots: [],
lines: [],
_id: '5f753118391fe067dcfa1c88',
shift: 'Shift A',
date: '2020-10-01T00:00:00.000Z',
rawData:
robot1_runTime: 4989,
robot2_runTime: 6956,
robot2_waitTime: 6944,
robot2_faultTime: 102,
skuLine4B_name: '0',
skuLine2_name: '32477045',
skuLine1_count: '0',
robot1_faultTime: 0,
robot1_waitTime: 4926,
dateReportingLastChange: '2020-09-30T23:14:51.400Z',
last_connection: '2020-09-30T23:14:51.400Z',
skuLine1_name: '0',
skuLine1_value: 0,
skuLine10_name: '0',
skuLine10_value: 0,
skuLine12_name: '32477213',
skuLine12_value: 77,
skuLine13_name: '32477063',
skuLine13_value: 52,
skuLine2_value: 249,
skuLine3_name: '32476914',
skuLine3_value: 1,
skuLine4A_name: '0',
skuLine4A_value: 0,
skuLine5_name: '68190192',
skuLine5_value: 6,
skuLine9_name: '0',
skuLine9_value: 0,
skuLine4B_value: 0
,
createdAt: '2020-10-01T01:30:00.918Z',
updatedAt: '2020-10-01T02:47:22.107Z',
__v: 0
obj = ...obj, ...obj.rawData;
delete obj.rawData;
console.log(obj);
【讨论】:
【参考方案2】:如果您在 js 中执行此操作,那么您可以执行以下操作(我假设您在 dataArr
变量中有一个此类对象的数组):
const dataArr = [
robots: [],
lines: [],
_id: "5f753118391fe067dcfa1c88",
shift: 'Shift A',
date: "2020-10-01T00:00:00.000Z",
rawData:
robot1_runTime: 4989,
robot2_runTime: 6956,
robot2_waitTime: 6944,
,
createdAt: "2020-10-01T01:30:00.918Z",
updatedAt: "2020-10-01T02:47:22.107Z",
__v: 0
,
robots: [],
lines: [],
_id: "5f753118391fe067dcfa1c88",
shift: 'Shift A',
date: "2020-10-01T00:00:00.000Z",
rawData:
robot1_runTime: 4989,
robot2_runTime: 6956,
robot2_waitTime: 6944,
,
createdAt: "2020-10-01T01:30:00.918Z",
updatedAt: "2020-10-01T02:47:22.107Z",
__v: 0
]
dataArr.forEach(obj =>
for(const key of Object.keys(obj.rawData))
obj[key] = obj.rawData[key];
delete obj.rawData;
)
console.log(dataArr);
【讨论】:
以上是关于在 API 的父对象中合并子对象的主要内容,如果未能解决你的问题,请参考以下文章