2.9 loperator spread and rest

We learned a lot about the functions of arrows classes now turning we head to new operators that we will be able to use in the future in javascript is done we can already used today in our project react the operators spread and remains in reality there is only one operator three points yes it may seem strange but the operator has only 3 points now otherwise lance spread or remains it depends on where we use the operator spread and use to split array elements or properties of the object

we have extended to array or an object for example if we have an old array and we want to add all the elements of this old array a new array is added plus an element 1 is an element 2 this first syntax would be the one we use three dots in front of the array simply extract all the elements and add it to a new array that we created with brackets and of course we can add more elements so if we just use the normal syntax with the brackets to create A table the same goes for the object we create a new object with braces with the new old object accessory to extract all the properties of the old objects and their values and add them as key-fathervalue to the new object as our complement to the old one.

also there is a new property it would be overwritten by a new accessory 5 here so our property to the priority is the operator propagation now the operator remains is the same but it is used differently it is used to merge with a list of arguments of function in an array and this shows us or using it we use in one of function arguments here is an example sortArgs is an unlimited number argument so an argument 2 3 or other with a point point we write that an argument but we can in receive several and they will all merge into an array so we can then apply array methods to our list of arguments to do what we want with our conveniently stored arguments let’s take a look. au 2 use as such on jsbin

so let’s start with the propagation operator I’m going to create an array of numbers 1 2 3 added as much as you want now I want to create a new array Newnumber and as you’ve seen spread is now just use by adding 3Points then the old ones tables numbers and potentially you don’t do it a new number like four and again With this if I console now the newspaper New issue we will put of course we will put it in English

Now if I click on Run it will put me 1 2 3 and 4 so this is the broadcast operator now the same for the objects I will create Person here or I have a name Here we are three small points person is young two point 21 is down we will mark console.log and we’ll put new person this is now if I record the console New person object age 21 years name Mathieu so it’s time to take the person and distribute it to the new person who is the propagation operator now the rest operator who used the less often and used in a function and you can of course use the arrow function es6 we could have a filter function as you call it

and we get some arguments without you having to use them here you could name whatever you want here but it is mandatory that there are three times here we will return the Args key and we will. A filter We will get here our element then we could use the built-in flash function to simply indicate the return as this is here we will put l = 1 can be that you did not know the value of the 3 equal signs the 3 signs checks the equality of the types and values so that it must also be a number all this has nothing to do with the rest or propagation of the shot down we will mark console.log but instead we will add this inside and as at the beginning if you remember well we will put a two three of course don’t forget the commas and now if we do run

This puts us one because we have filtered this table that writes with the operator rest with the three points here rest the propagation sound so the use of three points that you will see more often in this course.