javascript: object destructuring

时光总嘲笑我的痴心妄想 提交于 2021-01-29 07:58:45

问题


I have a this object:

{
  "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
}

and I would like to object destructing to this:

{
    "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; 

回答1:


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



回答2:


Destructuring doesn't build an object. You first need to destructure into local variables:

const { role, user: { uuid } } = userAuth;

then build the result from them:

const result = { role, uuid };

Alternatively, use a single statement without any destructuring:

const result = { role: userAuth.role, uuid: userAuth.user.uuid };



回答3:


You can have an IIFE that destructures the object and returns the destructured properties :

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);


来源:https://stackoverflow.com/questions/58367330/javascript-object-destructuring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!