Spread Operator

 

The spread operator can be utilized both on arrays and objects.

For arrays it will transform the array in a plain list of objects/variables, which can be utilized as input to functions that accept a list of arguments (example: array.push(el1, el2, el3). In This example if we have one array named array01 with options [1,2,3] and we want to add these values to another array, named array02, then we can do array02.push(…array01). Performing that, using the spread operator (…) actually the array02 will have a copy of all elements that are inside array01. Without using the spread operator , for example, array02.push(array01), the array02 will contains one single object that is the array01 itself, and only inside array01 that the values will be found.

Another applicability for spread operator is for objects itself. When we want to have a new object with contains a copy of all attributes an existing object already have, in addition to new attributes, we can use the spread operator. Lets see this example: the objectA has these attributes ({name: “my name”, address: “my address”}, then we want to create an objectB which has the same attributes as objectA in addition to the email. It could be accomplished by objectB={…objectA, email:”my email”}

You can see another examples and explanations at this video:



You can download an example of spread operator usage here:

https://github.com/rafaelqg/code/blob/main/spread_operator.js

Comments

Popular posts from this blog

HASHLIB: Using HASH functions MD5 and SHA256

Dart: implementing Object Oriented Programming (OOP)