Any way to make "const {user, userId} = this.state" work if user references username and userId references id

Viewed 704

for example

 state = { 
      "username": "abc",
      "id": "1",
 } 


 const {username, id} = this.state

the above code would work and get the values

is there a way to make it so below would also work

const {user, userId} = this.state

That is, get the same values as username, id but with different names. Any way to tell javascript that user means username and userId means id?

3 Answers

You can do like this if you just want to use different names:

const {username: user, id: userId} = this.state
Related