1

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": "[email protected]",
      "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; 
2
  • const { role, user: { uuid } } = userAuth; Commented Oct 13, 2019 at 20:00
  • const result = {role: userAuth.role, uuid: userAuth.user.uuid}; <--- isn't it much more readable in this very case? Commented Oct 13, 2019 at 20:39

3 Answers 3

4

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 };
Sign up to request clarification or add additional context in comments.

5 Comments

I can this: const [uuid, role] = [userAuth.user.uuid, userAuth]; but I think that your result is better
@reactdto Sure, you can write it out to const uuid = userAuth.user.uuid, role = userAuth.role; (no need for arrays), but the OP asked for destructuring
"You first need to destructure into local variables" --- or one could assign to a new object during destructuring: const result = {}; ({ role: result.role } = userAuth );
@zerkms Yeah, but that's much uglier than all the alternatives :-)
@Bergi in this particular case any destructuring is ugly and pointless :-). My idea was that if you wanted to cover destructuring features (given that "but the OP asked for destructuring") then adding that another sample would make it more complete :shrug:
0

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": "[email protected]",
          "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

3 Comments

Nie potrzebuję uuid z obiektu userAuth, tylko z user
już ktoś wyżej napisał to samo, ale dam Ci najlepszą odpowiedź bo widzę że dbasz o konto. Dzięki! ; )
Last line could be better changed to this: console.log({role, uuid}) - no need to repeat yourself.
0

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": "[email protected]",
      "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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.