javascript:对象解构 [英] javascript: object destructuring
本文介绍了javascript:对象解构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个此对象:
{
"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
}
我想反对破坏这一点:
{
"role": "WORKER_ROLE",
"uuid": "f0ca9c33-a5b7-48c1-9956-1d4d372475ec"
}
如何使数据对象解构出来呢?我尝试这样做:
how to make data object destructuring out of it? I try to this:
const { role, user.uuid } = userAuth;
推荐答案
这是示例答案.我认为,应该调用对象的userAuth属性.
This is sample answer. I think, that you should call userAuth property of your object.
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
这篇关于javascript:对象解构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文