Transcript
00:00 Okay. Let's jump into it. So we've got our scores right here. We want to grab the highest and the second highest scores. So with that, if I come down here, console log that, we get ninety five ninety two.
00:11 Great. So the way we do that is just with this open open bracket, not curly brace, an open bracket, we take the, in the first entry of that array looking syntax. It's not an array, but it looks like an array. That is going to be the variable name for the thing that you're plucking off. So it's just gonna take the first item out of the scores array and it's going to assign it to a variable called highest.
00:35 And then we'll take the second one and assign that to a variable called the second highest. And then we can actually, make this go further, to Here, let's actually We'll do this right here. We've got our winner and then we take the rest of, things and do others. Just like with object destructuring, you can just do dot dot dot. Here's the rest of the things and now you've got the rest of them.
00:59 So, that gets us our winner and the others. So there's the winner. There's the others. And now, let's take a look at coordinates. We're going to, destructure, the coordinates into x, y, and z.
01:13 And that, if we log that out, that's gonna do exactly what you would expect. So, same dealio there. And then we're gonna make a function called get min max. And the AI knows what we're trying to do here, so we'll just let it. Get min max is going to take numbers as an array.
01:34 I'm gonna rewrite this because it's just the only sane way to write that. We're going to spread the numbers on math dot min and math dot max and that's gonna give us the minimum and the maximum number of what's being provided in the array, and then it's gonna return this array with the min and the max. And we can then, get the min and the max calling get min max, passing that array, and it's gonna grab us those. So the point here is that we can actually, we take whatever this expression was, turns out that that returns an array, and we can destructure that. You could do the same thing with an object.
02:14 If we wanted to say, no, this is an object. These are named properties. You can, pluck those off and as long as the name matches the name that you're putting here, with the object syntax, then that is going to work just as well. The nice thing about the array syntax is that it's positional rather than the actual name. So this could be, my min and my max and that will still work because this one is positional whereas an object, goes by the key.
02:44 So the nice thing about the object, is because it goes by a key, it doesn't have to be in that same order. You'd say min and max, but you could actually do it in a separate order and that will work just fine because we're returning an object here. So coming back to the actual example of what we're trying to do, we've got our min and our max. We're gonna log those, save that, and we're getting, looks like this comment was wrong. It should be one and nine.
03:11 Thank you. Well, it's wrong based on the the inputs that, I gave it here. Actually, here, let's do scores. I think that's what we're supposed to do. Scores.
03:21 There we go. And, yep, it was right. 7995. There we go. Then we can export this for our tests and there, it looks lovely.
03:32 So that is yeah. That's that. That is pretty much all that you really need to know about destructuring arrays. They can be destructured, in, the positional or in, the parameters as well. I don't really ever do that.
03:49 Maybe if I had a coordinates, so like, get, max cord coordinates or something, and, here are my coordinates. I'm gonna do x, y, and z. I could see myself doing something like this, that that could make sense. But, yeah, I don't typically do that in the parameters, list for arrays. But I do sometimes return arrays like this, and we'll talk more about that a little bit later too.
04:17 So anyway, hopefully, that was super fun and interesting. Destructuring is pretty cool cool business here. So hope you enjoyed that.
