We also got useful features related to arrays and objects and how we can retrieve the data that's stored
in arrays and objects.
Now for that let's add a new array here.
Let's say hobbies and that can be an array of strings
like sports and cooking gear.
And now obviously we can retrieve elements by index here like this for example.
But you should know these basic javascript things.
Next Gen javascript introduced a nice feature if we want to extract all values of an array.
When could you need that.
But let's say you have the active hobbies and that's an array where I do have hiking as a hobby and
now I want to add sports and cooking.
Now we can add item two active hobbies by calling push by the way in case you're wondering how I can
push to a constant.
Keep in mind that Arrays are objects and objects are reference values.
When we push we change the memory but not the address.
If this doesn't tell you anything.
Check the additional resources I attached to this lecture regarding that topic.
So back to this example then we can push here but push.
If we push into hobbies we'll add hobbies as a new array in the arrays so as a new nested array which
types could actually detect that it tells me that if I want to add a string array to an array of strings
that's not okay.
So instead push takes single values.
So we could pass Hobbes 0 actually push even takes an unlimited amount of arguments so that we can push
multiple items in order at once like that but that's a bit cumbersome and that's where the spread operator
comes in.
It looks like this it's free dots and yes it does look strange but this is a valid javascript operator
supported by TypeScript and after dead we specify or we point at the array or object as he will also
learn that we want to spread.
So what this does is it tells JavaScript in the end because that's a vanilla javascript operator available
in modern javascript so it tells javascript to pull out all the elements of that array and basically
add them as a list of values so not as an array but a list of individual values in the place where you
used that operator.
So here for example is a list of arguments passed to push and no place where you could use Stead is
directly when you create a new array dear.
You can also spread an existing array into that array.
So basically whenever you need a comma separated list of values you can use the spread operator with
an array to get such a comma separated list.
That's the idea of the spread operator and therefore it's really useful for pulling out elements of
an array the spread operator.
All that does not just exist on arrays.
It also exists on objects.
Let's say we have a person which has a name key Max and that age key which is 30 and now we want to
create a copied person.
Now if we just assign purpose like this what we're doing is actually we're copying the pointer at this
person object in memory into this copied person constant and again check my reference value resources
attached if that doesn't tell you much so we're not really creating a copy of that object.
Now to create a real copy we can use the spread operator with objects we create a new object with curly
braces and then we use the spread operator free dots with our first object which you want to copy.
Now what this does is it pulls out all the elements but of course here the elements are key value pairs
not single values like in an array.
Here we get key value pairs so these key value pairs are pulled out of there and then can be used anywhere
where you need key value pairs which typically is a number object you might be adding them too so therefore
to this otherwise empty object the key value pairs of person are now added.
And since we created a new object here and just added to key value pairs we got a perfect copy of the
original object and not just the pointer that points to the object in memory.