3 回答
TA贡献1810条经验 获得超4个赞
解构不会构建对象。您首先需要解构为局部变量:
const { role, user: { uuid } } = userAuth;
然后从它们构建结果:
const result = { role, uuid };
或者,使用一个没有任何解构的语句:
const result = { role: userAuth.role, uuid: userAuth.user.uuid };
TA贡献1772条经验 获得超5个赞
这是示例答案。我认为,您应该调用对象的 userAuth 属性。
const obj = {
"userAuth": {
"id": 1,
"uuid": "e30fa23a-bfe4-495e-b6ac-79d49cb9a5a5",
"login": 12,
"password": "",
"role": "WORKER_ROLE",
"user": {
"id": 2,
"uuid": "f0ca9c33-a5b7-48c1-9956-1d4d372475ec",
"firstName": "Adrian",
"lastName": "Pietrzak",
"email": "test111@test.com",
"phone": null,
"avatar": null,
"street": "string",
"city": "string",
"state": "string",
"zip": "string",
"createdAt": "2019-10-12",
"lastLogin": "2019-11-29T20:03:17.000Z",
"lastLogout": null
}
},
"iat": 1570996289
}
const {role, user: {uuid} } = obj.userAuth;
console.log({role: role, uuid: uuid}) // this gives output, that you expect
TA贡献1906条经验 获得超3个赞
您可以使用 IIFE 来解构对象并返回解构后的属性:
const data = {
"userAuth": {
"id": 1,
"uuid": "e30fa23a-bfe4-495e-b6ac-79d49cb9a5a5",
"login": 12,
"password": "",
"role": "WORKER_ROLE",
"user": {
"id": 2,
"uuid": "f0ca9c33-a5b7-48c1-9956-1d4d372475ec",
"firstName": "Adrian",
"lastName": "Pietrzak",
"email": "test111@test.com",
"phone": null,
"avatar": null,
"street": "string",
"city": "string",
"state": "string",
"zip": "string",
"createdAt": "2019-10-12",
"lastLogin": "2019-11-29T20:03:17.000Z",
"lastLogout": null
}
},
"iat": 1570996289
}
const result = (({ role, user: { uuid } }) => ({role, uuid}))(data.userAuth);
console.log(result);
添加回答
举报