Heres how JavaScript's Nested Object Destructuring works

Поделиться
HTML-код
  • Опубликовано: 16 сен 2024
  • A quick video detailing how destructuring works with nested objects and arrays. Check out my ES6.io course for more!

Комментарии • 117

  • @JohannesBrodwall
    @JohannesBrodwall 6 лет назад +25

    Wow! The most productive minutes I've spent all month. Thanks! 😀

  • @shafiq_ramli
    @shafiq_ramli 9 месяцев назад +1

    Can't believe now I get how nested object destructuring works!
    Reading the doc on MDN confuse the heck out of me because they didn't mention that the parent variable becomes undefined when destructuring its child object property.
    Good stuff 👍🏻👍🏻

  • @MyDutchproductions
    @MyDutchproductions 4 года назад +2

    I needed this... Honestly I still need it, but I wish I had found these videos before writing an insane loop.

  • @moosecouture1
    @moosecouture1 6 лет назад +18

    This is also great when you combine it with Spread, Rest. Makes everything a lot cleaner

  • @KuroiKyu
    @KuroiKyu 6 лет назад +7

    Thanks Wes! Even when I think I know the topic I end up learning something

  • @jasdn93bsad992
    @jasdn93bsad992 6 лет назад +12

    3:29 destructure deeper
    7:29 destructure array

  • @javascriptwar9525
    @javascriptwar9525 4 года назад +1

    I was also facing this nested problems ... but from tomorrow no complain no demand
    Thankyou very much sir..🙌

  • @manuelzamorano2580
    @manuelzamorano2580 Год назад

    Thank you! I was so confused until I found this.

  • @notthere83
    @notthere83 6 лет назад +2

    I think it would've been great had you pointed out that one can of course write destructuring constructs across multiple lines. Because if you destructure nested objects and you write the destructuring the same way that you would write the object if you were to create it, it becomes very clear and easy to read.

  • @beingian
    @beingian 3 года назад

    You are amazing! i come back here at least once a week

  • @Shubham-jr8nc
    @Shubham-jr8nc 5 лет назад

    Best Explanation by An Advanced Teacher

  • @lpdrummerboy
    @lpdrummerboy Год назад

    Exactly what I was looking for. Thanks!

  • @TiagoSalemaG
    @TiagoSalemaG 6 лет назад +1

    Uaaau here I was all confused about this black magic fuckery just to find out it's actually rather simple and useful after watching this video.
    Kudos to Wes! You're the man.

  • @vincentiusherie
    @vincentiusherie 2 года назад

    Never thought this is possible. Crazy.

  • @erichepperlewp
    @erichepperlewp 2 года назад

    Nested destructuring...wow! SUBSCRIBED

  • @benyamin4634
    @benyamin4634 2 года назад

    cool tutorial so straightforward

  • @Afewwilliams
    @Afewwilliams 6 лет назад

    You're a legend Wes, thanks for all your effort 👍

  • @devincustodio2258
    @devincustodio2258 6 лет назад +28

    Seems like sacrificing readability at the expense of compactness.

    • @conoroflanagan2908
      @conoroflanagan2908 6 лет назад +2

      Devin Custodio true, me and a colleague were tripped up on variables of similar names + 2 helpings of destructing.

    • @devincustodio2258
      @devincustodio2258 6 лет назад

      Cono 52 but what do I know. I just have a degree in computer science. I'm not qualified at all to talk about this. 😂

    • @adrianli7757
      @adrianli7757 6 лет назад +11

      As someone with industry experience, I agree completely. Write code for others to read and understand, not for yourself to feel good about it.

    • @nazarm6215
      @nazarm6215 6 лет назад +5

      At some point you are going to have to learn and move on, otherwise readable code means you'll end up with stagnant progress in technological innovation.

    • @cardbored_
      @cardbored_ 2 года назад

      @@devincustodio2258 as someone with 20 years professional experience as a software engineer and now the director of the department at the agency I’m with, I’ve met many engineers with CS degrees who were half the programmer of some self taught developers. A degree doesn’t necessarily automatically mean someone is qualified in a real world position.

  • @pavanrao2965
    @pavanrao2965 4 года назад

    The most helpful video ever.period

  • @longtimedeveloper6498
    @longtimedeveloper6498 Год назад

    Thank you. I was immediately successful. Thank you!

  • @Opyext
    @Opyext 2 года назад

    this was very handy for me, thanks so much

  • @alan614
    @alan614 3 года назад

    This was great! Thanks for this Wes!

  • @jessereyes-cortes1194
    @jessereyes-cortes1194 Год назад

    Been using this technique for years... I finally know what the definition is haha

  • @writeandcodetime
    @writeandcodetime 5 лет назад

    I certainly learnt a thing or two :) You are an amazing teacher. Thanks you

  • @zdarofMERGAITES
    @zdarofMERGAITES 3 года назад

    Very well explained topic.

  • @raymonweave7157
    @raymonweave7157 5 лет назад

    Great video, well explained and loved to watch it!

  • @mparikshit
    @mparikshit 5 лет назад

    Just Superb :). I love the way you explained with super energy :)

  • @pratik66613
    @pratik66613 6 лет назад

    This is really useful. Thanks a ton Wes!

  • @MBehtemam
    @MBehtemam 6 лет назад

    Wow , Really Really amazing point that you mention , I love the way and I want to use it in my daily projects

  • @sergiyrudenko905
    @sergiyrudenko905 4 года назад

    Nice, really nice. Thanks Wes!

  • @emerson0001
    @emerson0001 6 лет назад

    I'm testing this out on console in the same window as I'm watching the video. I love how simple and neat this is.
    - I was wondering how I could back up a level to get a parent of something already destructed. Kind of dummie, but if any else is wondering:
    *var data = { foo: { bar: 1 } };*
    Say we want to get to *bar* but still want *foo* to be available:
    *const { foo, foo: { bar } } = data;*
    *> foo*
    _{bar: 1}_
    *> bar*
    _1_

  • @jordanpio2945
    @jordanpio2945 3 года назад

    Extremely helpful!!!

  • @nikicamaksimovski9964
    @nikicamaksimovski9964 6 лет назад +1

    I did not understand quite well how it helps in the example of react when you have to pass like 10 items as props.You will still have 10 lines of code.Any explanation ?

  • @rayraboypamisa1388
    @rayraboypamisa1388 5 лет назад +1

    Wes, How can I map through that nested array?

  • @boulderdennis
    @boulderdennis 6 лет назад +3

    Thanks Wes. One question. Are Canadian shoes normal or weird or something else?

    • @UliTroyo
      @UliTroyo 5 лет назад +2

      They're polite shoes!

  • @jordandoucet6887
    @jordandoucet6887 2 года назад

    This is super helpful 🙏

  • @denns0r
    @denns0r Год назад +1

    Although this is kind of cool, there are two things to consider: readabily is quite poor (which is my subjective opinion) but VERY IMPORTANT: THERE IS NO HANDLING OF UNDEFINED / FALLBACK VALUES. If you destructure only one level deep, you will get no errors if something is undefined, instead you can test it in ongoing code. When trying to destructure from undefined, you will get an error. So when working with uncertain data, like from an api, i would highly recommend NOT to do use deep nested destructuring!

  • @TrevorScanlan
    @TrevorScanlan 5 лет назад

    Wow. That was fantastic. Thank you

  • @rs14058
    @rs14058 6 лет назад

    This is super helpfull Wes!! 😎👍 thanks for sharing!

  • @fikrisidqi7115
    @fikrisidqi7115 3 года назад

    Great video, love it. Thank you sir!

  • @magdasokolovic
    @magdasokolovic 3 года назад

    great video. Love your enthusiasm :)

  • @redbeardhomes
    @redbeardhomes Год назад

    Can you show how to render these nested objects into a table? I am easily able to console.log those items but I am struggling with the display of the tables. I can't use the map() function.

  • @Souravbaranwal
    @Souravbaranwal 5 лет назад

    This was very helpful. Thanks a lot.

  • @narwhalboobs
    @narwhalboobs 6 лет назад +1

    Hey Wes, I was wondering what Visual Studio Code plugin you're using to display comments and attributes in a different font.

    • @WesBos
      @WesBos  6 лет назад

      I wrote a bit of info here → wesbos.com/uses

  • @alirezamousavi7939
    @alirezamousavi7939 6 лет назад

    That was a great video. very useful. thanks bro.

  • @kamalpanhwar4487
    @kamalpanhwar4487 6 лет назад +1

    Your font in editor is great, what is name of that font and color scheme? as I searched many but nothing resembling this font
    typeface.

    • @UliTroyo
      @UliTroyo 5 лет назад +1

      I just found it on Wes's blog; it's Operator Mono, but it's $200.

  • @Dan-codes
    @Dan-codes 3 года назад

    Thank you.

  • @sumantkanala
    @sumantkanala 6 лет назад

    You go Wes!

  • @shaunakthakar3026
    @shaunakthakar3026 3 года назад

    Great Video. Have been looking for this. Can someone help me with this question.
    How can i just pull only name from the items in the array. I don't need the other data just all the names inside the array.

  • @GiovanniOrlandoi7
    @GiovanniOrlandoi7 6 лет назад

    This is great. Thank you!

  • @jaydeepkarena
    @jaydeepkarena 6 лет назад

    really good explanation 🎉

  • @dragannovakovic9816
    @dragannovakovic9816 6 лет назад +2

    5:15 a Wild render prop Appears xD

  • @Jay-eg7yt
    @Jay-eg7yt 5 лет назад

    Love the display. Especially how the attributes are in italics. Which them are you using?

  • @sreenivasulugattu2936
    @sreenivasulugattu2936 4 года назад

    Thanks for the useful one.
    what if object is nested very deeply ?
    i mean more than 10 items deeper ??

  • @3ombieautopilot
    @3ombieautopilot 6 лет назад

    And we can also provide a default value for keys that are missing.

  • @amabenuzoe250
    @amabenuzoe250 3 года назад

    Thanks Wes. I've been trying to send nested form input values using the window.fetch API, do I destructure the inputs or just send the form values as is?

  • @ramin9134
    @ramin9134 3 года назад

    really amazing ;)

  • @JosephChambers
    @JosephChambers 6 лет назад

    Thanks Wes!

  • @AbhishekKumar-mq1tt
    @AbhishekKumar-mq1tt 6 лет назад

    Thank u for this awesome video

  • @BowlineDandy
    @BowlineDandy 6 лет назад

    Would be nice if that nested destructuring worked on imports specifically when importing json since Webpack can now tree shake those

  • @TheNinad22
    @TheNinad22 4 года назад

    i am having difficulty destructuring this array
    I want output as R8
    below is array
    var obj = [
    {
    title: "buy meal",
    isDone: false,
    },
    {
    title: "come office on time",
    isDone: false,
    },
    {
    title: [
    "BMW",
    [
    { id: 01, name: "R8" },
    { id: 02, name: "r7" },
    ],
    "maruti",
    ],
    isDone: true,
    },
    ];
    can you help me?

  • @gauravdawg
    @gauravdawg 5 лет назад

    can you suggest some good plugin for formatting the JSX code in VSCode ? I have components within components and each component have their own props, its such a waste of time manually pressing tab to fix the formatting to improve readability

  • @Peter-ur8nv
    @Peter-ur8nv 5 лет назад

    Very helpful, thank you

  • @imhappynow8471
    @imhappynow8471 4 года назад

    thank you

  • @lynxlagoon
    @lynxlagoon 3 года назад

    dude, thanks

  • @nicholassingh138
    @nicholassingh138 2 года назад

    hey, how would destructor the event_general part:
    const events = {
    event_id: 57,
    name: 'Event Name',
    organiser: 'John',
    category: 'Music',
    event_general: [
    {
    venue: 'unknown',
    country: '2',
    isVirtual: true,
    story: 'null'
    }
    ]
    };

  • @julianklumpers
    @julianklumpers 6 лет назад

    You can even omit properties/keys with rest spread and destructering.
    const { status, ...rest } = makeItem()
    Rest now only contains data.

    • @Nemcoification
      @Nemcoification 5 лет назад

      Just in case anyone's wondering, the same is true for arrays like: const [ status, ...rest ] = makeItem(); Arrays have the added benefit of just being able to skip the variable entirely by just doing const [ , ...rest ] = makeItem();.

  • @hsngsoftware4380
    @hsngsoftware4380 3 года назад

    How to get only second element in the array ?

  • @dan-3268
    @dan-3268 5 лет назад

    Beautiful ❤️

  • @sundarraaj2944
    @sundarraaj2944 Год назад

    How to add an object to a nested array of objects dynamically using distinct properties of an object

  • @vaasudhand3200
    @vaasudhand3200 4 года назад

    How do I rename the item1 and item2 variables?

  • @SinaGilassi
    @SinaGilassi 4 года назад

    Cool!

  • @batuhankir8402
    @batuhankir8402 2 года назад

    how can u make a table with all keys end values

  • @sagiriizumi8284
    @sagiriizumi8284 6 лет назад

    Thanks! :)

  • @kissu_io
    @kissu_io 6 лет назад +2

    Siiiiiiick video, sick sick sick. 💪🏻

  • @duckhorse2563
    @duckhorse2563 4 года назад

    Asante.

  • @alialqahtani7841
    @alialqahtani7841 6 лет назад

    good video
    thanks Wes Bos

  • @teenytinytoons
    @teenytinytoons 3 года назад

    You may have to play this video at half the speed.

  • @philipdegendt56
    @philipdegendt56 3 года назад

    Super 👍👌

  • @germantoenglish898
    @germantoenglish898 Год назад

    If the parent is an array with a nested object or visa versa and their children are arrays nested in objects or visa versa, it can all be so overwhelming and confusing.

  • @egougwu1123
    @egougwu1123 5 лет назад

    how do u destructure into a variable please?

  • @JoeWong81
    @JoeWong81 5 лет назад

    thanks bro

  • @hitheshkaverappa8634
    @hitheshkaverappa8634 4 года назад

    What if i need 2nd element of array (item2)

  • @WebDevProfesh
    @WebDevProfesh 6 лет назад +3

    Is there a way to only destructure the second item in an array, without destructuring the first?

    • @abdulazizalabodi
      @abdulazizalabodi 6 лет назад +4

      yes, arrays are just objects in js. so you could do const {1:s2}= [1,2,3]

    • @WesBos
      @WesBos  6 лет назад +5

      const [,secondItem] = someArray;

    • @davlewis2011
      @davlewis2011 6 лет назад +1

      So if I wanted the like the 10th index, I’d have to put 9 ‘, ‘ and then my variable?

    • @abdulazizalabodi
      @abdulazizalabodi 6 лет назад +2

      no look at my answer.

    • @CoryTheSimmons
      @CoryTheSimmons 6 лет назад

      Really cool Abdulaziz, thanks.

  • @navinkandpal6767
    @navinkandpal6767 2 года назад

    Good 👍

  • @simonkalu
    @simonkalu 5 лет назад

    True Bos

  • @alexpro5670
    @alexpro5670 2 года назад

    Деструктуризация объектов и массивов

  • @augischadiegils.5109
    @augischadiegils.5109 Год назад

    cool 😎

  • @justinoneill2837
    @justinoneill2837 6 лет назад +1

    Hi Wes, thanks a ton for this breakdown. I've still got a little confusion on how you make multiple variables from the same function and/or data set? For example, let's say I wanted to make *4 different variables* from the *makeItem()* function... how could I do that w/ out having to make *4 different makeItem()* functions?
    I put together a codepen here: codepen.io/oneezy/pen/VXWLXa?editors=0011

    • @moosecouture1
      @moosecouture1 6 лет назад

      I do not see where your codepen is showing any changes... but if I understand your question... if it is like what is being shown here with destructuring... you could pass it an array and destructure the array `const [ one, two, three, four ] = [makeItem(), makeItem(), makeItem(), makeItem()]` Otherwise I do not know how it falls into the scope of this quick tutorial.

    • @ethanstewart8957
      @ethanstewart8957 6 лет назад

      Not sure I fully understand the question. Are you talking about having data, item, etc. also be variables when you're four levels deep?

  • @Opyext
    @Opyext 2 года назад

    ur so cool

  • @tubehelpr
    @tubehelpr 4 года назад

    Neat but pretty hairy!