JavaScript Curriculum
On this page
- Destructuring & Rest/Spread
- Destructuring and Spread
- Destructing Arrays
- Destructuring during iteration
- Destructuring Object
- Renaming during structuring
- Object parameter without destructuring
- Object parameter with destructuring
- Destructuring object during iteration
- Spread or Rest Operator
- Spread operator to get the rest of array elements
- Spread operator to copy array
- Spread operator to copy object
- Spread operator with arrow function
- Exercises
- Exercises: Level 1
- Exercises: Level 2
- Exercises: Level 3
Curriculum sourced from JavaScript by Asabeneh Yetayeh.
Destructuring & Rest/Spread
- Day 11
- Destructuring and Spread
- Destructing Arrays
- Destructuring during iteration
- Destructuring Object
- Renaming during structuring
- Object parameter without destructuring
- Object parameter with destructuring
- Destructuring object during iteration
- Spread or Rest Operator
- Spread operator to get the rest of array elements
- Spread operator to copy array
- Spread operator to copy object
- Exercises
- Destructuring and Spread
Destructuring and Spread
Destructuring is a way to unpack arrays, and objects and assigning to a distinct variable.
Destructing Arrays
If we like to skip on of the values in the array we use additional comma. The comma helps to omit the value at that specific index
We can use default value in case the value of array for that index is undefined:
We can not assign variable to all the elements in the array. We can destructure few of the first and we can get the remaining as array using spread operator(...).
Destructuring during iteration
Destructuring Object
When we destructure the name of the variable we use to destructure should be exactly the same as the key or property of the object. See the example below.
Renaming during structuring
If the key is not found in the object the variable will be assigned to undefined. Sometimes the key might not be in the object, in that case we can give a default value during declaration. See the example.
Destructuring keys as a function parameters. Let us create a function which takes a rectangle object and it returns a perimeter of a rectangle.
Object parameter without destructuring
Object parameter with destructuring
Destructuring object during iteration
Spread or Rest Operator
When we destructure an array we use the spread operator(...) to get the rest elements as array. In addition to that we use spread operator to spread array elements to another array.
Spread operator to get the rest of array elements
Spread operator to copy array
Spread operator to copy object
We can copy an object using a spread operator
Modifying or changing the object while copying
Spread operator with arrow function
Whenever we like to write an arrow function which takes unlimited number of arguments we use a spread operator. If we use a spread operator as a parameter, the argument passed when we invoke a function will change to an array.
🌕 You achieved quite a lot so far. Now, your level of JavaScript is upper intermediate. Keep going! You have just completed day 11 challenges and you are 11 steps a head in to your way to greatness. Now do some exercises for your brain and for your muscle.
Exercises
Exercises: Level 1
- Destructure and assign the elements of constants array to e, pi, gravity, humanBodyTemp, waterBoilingTemp.
- Destructure and assign the elements of countries array to fin, est, sw, den, nor
- Destructure the rectangle object by its properties or keys.
Exercises: Level 2
- Iterate through the users array and get all the keys of the object using destructuring
- Find the persons who have less than two skills
Exercises: Level 3
- Destructure the countries object print name, capital, population and languages of all countries
- A junior developer structure student name, skills and score in array of arrays which may not easy to read. Destructure the following array name to name, skills array to skills, scores array to scores, JavaScript score to jsScore and React score to reactScore variable in one line.
- Write a function called convertArrayToObject which can convert the array to a structure object.
- Copy the student object to newStudent without mutating the original object. In the new object add the following ?
- Add Bootstrap with level 8 to the front end skill sets
- Add Express with level 9 to the back end skill sets
- Add SQL with level 8 to the data base skill sets
- Add SQL without level to the data science skill sets
The copied object output should look like this:
🎉 CONGRATULATIONS ! 🎉