合并和重组对象



我生成的对象是这样的:

[{
"device": {
"_id": "5d25f9d2dc4aea7838b0aaa1",
"serialNumber": "100000100215",
"patientRiskStatus": "high",
"createdAt": "2017-04-16T01:22:00.000Z",
"updatedAt": "2020-01-01T05:29:19.632Z"
},
"firstName": "userName"
}]

我想将此对象结构转换为:

[{
"_id": "5d25f9d2dc4aea7838b0aaa1",
"serialNumber": "100000100215",
"firstName": "userName",
"patientRiskStatus": "high",
"createdAt": "2017-04-16T01:22:00.000Z",
"updatedAt": "2020-01-01T05:29:19.632Z",
}]

这是我的代码——

var filteredDevices = []
var result = []
if (user.role === 'Nurse') {
devices.forEach((device) => {
filteredDevices.push({
device: device,
firstName: "userName"
})
result = filteredDevices.map(({ device, ...rest }) => ({
...rest,
...device
}))
})
}

我尝试了下面建议的解决方案,它在代码片段示例中看起来不错.js但我的节点只是行为不同,我不确定为什么。

我在尝试解决方案时得到的结果如下。 不知道为什么它给了我这么长的结果。

[
{
"firstName": "Rikin",
"__parentArray": [
{
"_id": "5d25f9d2dc4aea7838b0aaa1",
"serialNumber": "100000100215",
"area": "Office 1",
"connectionStatus": 0,
"gatewayKey": "gk_d0202d93d0d29293a1",
"applicationNumber": 40,
"firmwareVersion": "v12.1.8",
"needsAttention": true,
"verificationCode": "4144",
"patientRiskStatus": "high",
"createdAt": "2017-04-16T01:22:00.000Z",
"updatedAt": "2020-01-01T05:29:19.632Z"
},
{
"_id": "5d25f9d2dc4aea7838b0aaa2",
"serialNumber": "100000100216",
"area": "Office 2",
"connectionStatus": 0,
"gatewayKey": "gk_d0202d93d0d29293a1",
"applicationNumber": 41,
"firmwareVersion": "v12.1.8",
"needsAttention": true,
"verificationCode": "4145",
"patientRiskStatus": "low",
"createdAt": "2017-04-16T01:22:00.000Z",
"updatedAt": "2017-04-16T01:22:00.000Z"
},
{
"_id": "5d25f9d2dc4aea7838b0aaa3",
"serialNumber": "100000100217",
"area": "Office 4",
"connectionStatus": 0,
"gatewayKey": "gk_d0202d93d0d29293a1",
"applicationNumber": 60,
"firmwareVersion": "v12.1.8",
"needsAttention": true,
"verificationCode": "4146",
"patientRiskStatus": "low",
"createdAt": "2017-04-16T01:22:00.000Z",
"updatedAt": "2017-04-16T01:22:00.000Z"
}
],
"__index": 0,
"$isDocumentArrayElement": true,
"$__": {
"strictMode": true,
"getters": {},
"_id": "5d25f9d2dc4aea7838b0aaa1",
"wasPopulated": false,
"activePaths": {
"paths": {
"_id": "init",
"serialNumber": "init",
"area": "init",
"connectionStatus": "init",
"gatewayKey": "init",
"applicationNumber": "init",
"firmwareVersion": "init",
"needsAttention": "init",
"verificationCode": "init",
"patientRiskStatus": "init",
"createdAt": "init",
"updatedAt": "init"
},
"states": {
"ignore": {},
"default": {},
"init": {
"_id": true,
"serialNumber": true,
"area": true,
"connectionStatus": true,
"gatewayKey": true,
"applicationNumber": true,
"firmwareVersion": true,
"needsAttention": true,
"verificationCode": true,
"patientRiskStatus": true,
"createdAt": true,
"updatedAt": true
},
"modify": {},
"require": {}
},
"stateNames": [
"require",
"modify",
"init",
"default",
"ignore"
]
},
"pathsToScopes": {},
"cachedRequired": {},
"$setCalled": {},
"ownerDocument": {
"apiCallCount": 0,
"_id": "58dc5b9f3107602dbaba1281",
"email": "rikyshah@gmail.com",
"password": "password",
"userName": "rikyshah",
"companyName": "RS company",
"apiKey": "Hhd87AD&jasd90dlK09aosdO",
"buildings": [
{
"gateways": [
{
"devices": [
{
"_id": "5d25f9d2dc4aea7838b0aaa1",
"serialNumber": "100000100215",
"area": "Office 1",
"connectionStatus": 0,
"gatewayKey": "gk_d0202d93d0d29293a1",
"applicationNumber": 40,
"firmwareVersion": "v12.1.8",
"needsAttention": true,
"verificationCode": "4144",
"patientRiskStatus": "high",
"createdAt": "2017-04-16T01:22:00.000Z",
"updatedAt": "2020-01-01T05:29:19.632Z"
},
{
"_id": "5d25f9d2dc4aea7838b0aaa2",
"serialNumber": "100000100216",
"area": "Office 2",
"connectionStatus": 0,
"gatewayKey": "gk_d0202d93d0d29293a1",
"applicationNumber": 41,
"firmwareVersion": "v12.1.8",
"needsAttention": true,
"verificationCode": "4145",
"patientRiskStatus": "low",
"createdAt": "2017-04-16T01:22:00.000Z",
"updatedAt": "2017-04-16T01:22:00.000Z"
},
{
"_id": "5d25f9d2dc4aea7838b0aaa3",
"serialNumber": "100000100217",
"area": "Office 4",
"connectionStatus": 0,
"gatewayKey": "gk_d0202d93d0d29293a1",
"applicationNumber": 60,
"firmwareVersion": "v12.1.8",
"needsAttention": true,
"verificationCode": "4146",
"patientRiskStatus": "low",
"createdAt": "2017-04-16T01:22:00.000Z",
"updatedAt": "2017-04-16T01:22:00.000Z"
}
],
"_id": "5d254b64ba574040d9632ada",
"gatewayName": "RBC-xaka-001",
"gatewayKey": "gk_d0202d93d0d29293a1",
"suite": "RBC1",
"createdAt": "2017-04-16T01:22:00.000Z",
"updatedAt": "2020-01-01T05:29:19.632Z"
},
{
"devices": [
{
"_id": "5d25f9d2dc4aea7838b0aaa0",
"serialNumber": "100000100214",
"area": "Office 3",
"connectionStatus": 0,
"gatewayKey": "gk_d0202d93d0d29293a2",
"applicationNumber": 60,
"firmwareVersion": "v12.1.8",
"needsAttention": true,
"verificationCode": "4147",
"patientRiskStatus": "low",
"createdAt": "2017-04-16T01:22:00.000Z",
"updatedAt": "2017-04-16T01:22:00.000Z"
}
],
"_id": "5d254b7174d7b3a73dff342f",
"gatewayName": "RBC-xaka-002",
"gatewayKey": "gk_d0202d93d0d29293a2",
"suite": "RBC2",
"createdAt": "2017-04-16T01:22:00.000Z",
"updatedAt": "2017-04-16T01:22:00.000Z"
}
],
"_id": "5d254bb179584ebcbb68b712",
"buildingName": "RBC Bank Tower",
"address": "1 Front Street East",
"suite": "110",
"floor": "2nd floor",
"timeZone": "America/Toronto",
"createdAt": "2017-04-16T01:22:00.000Z",
"updatedAt": "2020-01-01T05:29:19.632Z"
}
],
"createdAt": "2017-04-16T01:22:00.000Z",
"updatedAt": "2020-01-01T05:29:19.632Z"
},
"fullPath": "buildings.gateways.devices",
"emitter": {
"_events": {},
"_eventsCount": 2,
"_maxListeners": 0
},
"$options": {}
},
"isNew": false,
"_doc": {
"_id": "5d25f9d2dc4aea7838b0aaa1",
"serialNumber": "100000100215",
"area": "Office 1",
"connectionStatus": 0,
"gatewayKey": "gk_d0202d93d0d29293a1",
"applicationNumber": 40,
"firmwareVersion": "v12.1.8",
"needsAttention": true,
"verificationCode": "4144",
"patientRiskStatus": "high",
"createdAt": "2017-04-16T01:22:00.000Z",
"updatedAt": "2020-01-01T05:29:19.632Z"
},
"$locals": {},
"$init": true
},.....

使用Array.map()迭代数组。在回调中,解构device属性,并使用 rest 语法获取对象的其余部分。

通过将device和原始对象的其余部分展开到新对象中来重建对象。

const arr = [{"device":{"_id":"5d25f9d2dc4aea7838b0aaa1","serialNumber":"100000100215","patientRiskStatus":"high","createdAt":"2017-04-16T01:22:00.000Z","updatedAt":"2020-01-01T05:29:19.632Z"},"firstName":"userName"}]

const result = arr.map(({ device, ...rest }) => ({
...rest,
...device
}))
console.log(result)

最新更新