Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Raúl Sánchez
Raúl Sánchez

Posted on

     

Destructuring with an alias

Short and simple.

constbar={x:5,};const{x:foo}=bar;console.log(foo);// 5
Enter fullscreen modeExit fullscreen mode

This can be useful when you need to destructure a value that has a similar or same name as an existing variable. E.g.,

constcars=[{car:true,van:false,make:'Honda',year:2001,color:'Red',},{car:true,van:false,make:'Nissan',year:2000,color:'Blue',}];cars.map(car=>{const{car:isCar}=car;console.log(isCar);});
Enter fullscreen modeExit fullscreen mode

Top comments(2)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
pdina profile image
Paolo E Basta
enthusiast software developer
  • Joined

This is very nice and works well. I just wonder where this alias stuff is officially documented, because I found anything (except some post on StackOverflow but without any official reference).

CollapseExpand
 
rsanchezp profile image
Raúl Sánchez
  • Location
    Denver, CO
  • Education
    B.S. of Computer Science @ University of Denver
  • Work
    Software Engineer Lead
  • Joined

You can find it on the officialMDN site. It's under a section called Assigning to new variable names

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

  • Location
    Denver, CO
  • Education
    B.S. of Computer Science @ University of Denver
  • Work
    Software Engineer Lead
  • Joined

More fromRaúl Sánchez

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp