Check out a free preview of the full JavaScript: The Recent Parts course
The "Nested Object Destructuring" Lesson is part of the full, JavaScript: The Recent Parts course featured in this preview video. Here's what you'd learn in this lesson:
Kyle demonstrates how to access a returned data structure's values when it is known that there are objects.
Transcript from the "Nested Object Destructuring" Lesson
[00:00:00]
>> Kyle Simpson: Okay, what if our object has a sub object in it? Like if I have an a1, and then I have b, includes my c and d properties.
>> Kyle Simpson: Well, what is B gonna point at here? It's gonna point at the whole object. If I wanted C to point at C, then I would end up saying temp.
[00:00:41]
I would end up saying b.c. And d = b.d. So if I had that same kind of object structure being returned into my destructuring pattern,
>> Kyle Simpson: I'm going to say the source, the property we're getting it from is called b. And what is my target, it is another destructuring pattern.
[00:01:14]
And what is in that destructuring pattern? It is that I want C to point at C, which in this case, I can just list shorthand, and D to point at D, which in this case can just be the shorthand. And what happens if B doesn't exist? I have to make that default to an empty object, and the same is true over here in my destructuring pattern.
[00:01:47]
I need a default for this position, and that default is the empty object.
>> Kyle Simpson: It is extremely easy to forget those defaults, especially on your nested patterns. And you'll get type errors blowing you up when you forget them, and it is not gonna get any easier to remember it, at least in my experience, I still forget them.
[00:02:15]
So I'm gonna strongly recommend you use a lenting role to remind you to put those defaults in, and ES lent has a lenting role that reminds you need defaults on your nested patterns.
Learn Straight from the Experts Who Shape the Modern Web
- In-depth Courses
- Industry Leading Experts
- Learning Paths
- Live Interactive Workshops