What is Destructuring ?
Syntax in array and object?
extract specific values from complex data structures like arrays or objects and assign them to variables
const [x, y, z] = [1, 2, 3];
console.log(x, y, z); // 1 2 3
const { variable 1, variable 2} = objectDestructuring’s Features for Array
const restaurant = {
name: 'Classico Italiano',
location: 'Via Angelo Tavanti 23, Firenze, Italy',
categories: ['Italian', 'Pizzeria', 'Vegetarian', 'Organic'],
starterMenu: ['Focaccia', 'Bruschetta', 'Garlic Bread', 'Caprese Salad'],
mainMenu: ['Pizza', 'Pasta', 'Risotto'],
};1/ Extract ‘Italian’ and ‘Vegetarian’ from categories into main and secondary variable, then mutate them by switch between 2 values
2/ Write a order method so that when input numbers (1,2), return array contains food’s name in Starter Menu and Main Menu.
3/ Call the method and Destruct returned array into variables ‘starter’ and ‘mainCourse’
1 /
// Print 1st and 3rd in categories and assign them in main, secondary variable. Skip 2rd category
let [main, , secondary] = restaurant.categories;
console.log(main, secondary); // Italian Vegetarian
// Switch Vegetarian to main and Italian to secondary
// Switch variables - Old trick, set a temp variable to hold main value
const temp = main;
main = secondary;
secondary = temp;
console.log(main, secondary); // Vegetarian Italian
// Switch variables - New trick using destructuring
[main, secondary] = [secondary, main];
console.log(main, secondary); // Vegetarian Italian"
2/
const restaurant = {
name: 'Classico Italiano',
location: 'Via Angelo Tavanti 23, Firenze, Italy',
categories: ['Italian', 'Pizzeria', 'Vegetarian', 'Organic'],
starterMenu: ['Focaccia', 'Bruschetta', 'Garlic Bread', 'Caprese Salad'],
mainMenu: ['Pizza', 'Pasta', 'Risotto'],
// write a order method so when put an index number, it returns starterMenu and main Menu in array
order: function (starterIndex, mainIndex) {
return [this.starterMenu[starterIndex], this.mainMenu[mainIndex]];
},
};
// Receive 2 return values in array from a function then destructure them into variables
const [starter, mainCourse] = restaurant.order(2, 0);
console.log(starter, mainCourse); // Garlic Bread, Pizzaconst arr = [1, 2, [4, 5], 6];
Assign 1,4,5,6 to variables a,c,d,e,f. If any variable is undefined, set it with default value of 100
const [a, , [c, d], e, f = 100] = arr; console.log(a, c, d, e, f);
Destructuring’s Features for Object
const restaurant1 = {
name: 'Classico Italiano',
location: 'Via Angelo Tavanti 23, Firenze, Italy',
categories: ['Italian', 'Pizzeria', 'Vegetarian', 'Organic'],
starterMenu: ['Focaccia', 'Bruschetta', 'Garlic Bread', 'Caprese Salad'],
mainMenu: ['Pizza', 'Pasta', 'Risotto'],
realOwner: 'Vu Son',
realAge: 27,
openingHours: {
thu: {
open: 12,
close: 22,
},
fri: {
open: 11,
close: 23,
},
sat: {
open: 0, // Open 24 hours
close: 24,
},
},
};
let owner = 'Giovani';
let age = 40;
// 1. Classico Italiano Via Angelo Tavanti 23, Firenze, Italy {thu: {…}, fri: {…}, sat: {…}}
2. [] (4)['Focaccia', 'Bruschetta', 'Garlic Bread', 'Caprese Salad']
3. Vu Son 27
// 1.
const {
name: restaurantName,
location: address,
openingHours: hours,
} = restaurant1;
// console.log(restaurantName, address, hours); // Classico Italiano Via Angelo Tavanti 23, Firenze, Italy {thu: {…}, fri: {…}, sat: {…}}
// 2.
const { menu = [], starterMenu = [] } = restaurant1;
// console.log(menu, starterMenu); // [] (4)['Focaccia', 'Bruschetta', 'Garlic Bread', 'Caprese Salad']
// 3.
({ realOwner: owner, realAge: age } = restaurant1);
// console.log(owner, age); // Vu Son 27
// 4.
// OPTION 1
// const { open: openTime, close: closeTime } = restaurant1.openingHours.thu;
// OPTION 2
const {
openingHours: {
thu: { open: openTime, close: closeTime },
},
} = restaurant1;
console.log(openTime, closeTime);
// 5.
// put this method inside object restaurant and call method
orderDelivery({ time, address, starterIndex, mainIndex }) {
console.log(
`${this.starterMenu[starterIndex]} and ${this.mainMenu[mainIndex]} will be delivered to ${address} at ${time}`
);
},Definition
The Spread Operator
The spread operator is used to spread elements from an iterable (like an array or a string) into another array, function arguments, or object literals.
Note: When using the spread operator, you need to provide a target (like a new array, function call, or object literal) to receive the spreaded elements.
How to turn array into values separated by commas?
The Spread Operator