Let's stick to arrays and objects and no important nice syntax which you'll see a lot in modern javascript
and also types good projects is a ray and object D structuring how does that work.
Well let's start with our array which we got the Hobbes array which gets to Hobbes let's say you want
to extract these two hobbies and store them in variables or constants.
Of course we can do that with Hobby one equal to Hobby one equal hobbies for index 0 which is the first
element and the same maybe with hobby too for hobbies at index 1.
This will pull out these two elements and store them in constants.
It took us two lines of code and the more elements we want to retrieve the more lines of code.
It takes us a radio structuring allows us to shorten that with array D structuring which also works
with let by the way not just with const you add square brackets on the left side of the equal sign which
is an unusual place.
Haven't seen that before.
And on the right side you have the array which you want to d structure now D structure really means
stead you pull elements out of the array and here on the left side between these square brackets you
store them in constants or if you use left in variables.
So here we could then have how we won and hobby too and what the syntax does is it goes through the
Hobbes array takes the first element and stores it in a constant with this name takes the second element
stores is it in a constant with this name you can even add rest parameters here and have remaining Hobbes
here in case do you Hobbes array had more elements than just two then all the remaining elements which
you didn't pull out into a hobby one or hobby two will be stored in a new array called remaining hobbies
so they will be merged together into a new array by the way these structuring does not change to your
original array so if we console log hobbies hobby 1 and hobby to here you will see that Hobbes is still
the old array it's not empty or anything like dead.
So the values which we pulled out in quotes are really just copied into new constants or variables not
really removed from the array.
Now that's a radio structuring we've got the same four objects we got our person here.
Now let's say we want to store name and age in standalone constants or variables just as a radio structuring
object restructuring works with both constant let him go with concept and now we can use curly braces
on the left side of the equals sign although a new syntax I guess and have our object or the variable
pointing at the object on the right side now by the way please note this is not an object type I'm assigning
here I don't have a colon here is read just curly braces like this on the left side without a colon
after some constant name so I'm not assigning the type of the value that could be stored in a constant
I'm really just using a special syntax because here we now also can refer to property names we have
an add object for example name and age to pull the values for these keys out of person and store them
in constants of the same name now here I'm getting errors because at the very top I already have a h
variable and name turns out to be a globally available variable which exists anyways.
So there work around would be to really just use a different key name here first name for example.
And now we can pull out the value by that key here.
So this is how object destruction works.
The important thing here is for a radius structuring elements are pulled out in order because an array
is an ordered list in objects.
The order is not always guaranteed and therefore we don't pull elements out by position but by key name.
So the values or the names you specify here between the curly braces are not arbitrary.
You can't come up with names here.
These have to be property names you find in the object so that the values for these keys can be pulled
out of that object and then they're stored in constants or variables of the same name.
If you would want to override dad name you could do so with a cold in here.
Again it's not a type assignment here.
It just sets and you name.
So this is all javascript syntax has nothing to do with typescript.
So here we could store this in user name consent for example and therefore thereafter we can console
log user name and age but not first name because it basically overrode first name to be named user name
in the constant which we create when we pull out values here.
Age is not overwritten so we can use it like that again does is not a type assignment here.
This is javascript syntax where we alias rename property we're pulling out a person all of Justice before
with the array the person object itself is not changed we really just copy these values out of the object
and therefore we get this output here.