Javascript 合并和重组对象
我的结果对象如下所示:Javascript 合并和重组对象,javascript,node.js,Javascript,Node.js,我的结果对象如下所示: [{ "device": { "_id": "5d25f9d2dc4aea7838b0aaa1", "serialNumber": "100000100215", "patientRiskStatus": "high", "createdAt": "2017-04-16T01:22:00.000Z", "updatedAt": "2020-01-01T05:29:19.632Z" }, "firstName": "us
[{
"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
}))
})
}
我尝试了下面建议的解决方案,它在代码段示例中看起来不错,但我的node.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()
迭代数组。在回调中,解构设备
属性,并使用rest语法获取对象的其余部分
通过将设备
和原始对象的其余部分展开成新对象来重建对象
const arr=[{“device”:{“id”:“5D25F9D2DC4AEA783800AAA1”,“serialNumber”:“10000000215”,“patientRiskStatus”:“high”,“createdAt”:“2017-04-16T01:22:00.000Z”,“updatedAt”:“2020-01-01T05:29:19.632Z”},“firstName”:“userName”}]
const result=arr.map(({device,…rest})=>({
休息
…装置
}))
console.log(result)
代码片段看起来很完美,但当我粘贴问题时,我的node.js代码只给出了很长的结果。请注意,这会创建一个新数组,并且不会改变原始数组。原始输入结构和您在问题中显示的内容并不相同。此外,您正在使用user
对象过滤数据,该对象未出现在问题中。我建议您打开一个新问题,添加原始输入、原始代码和所需输出。