Skip to content

Commit 0749313

Browse files
committed
complete destructuring arrays and objects
1 parent 71ea4de commit 0749313

File tree

4 files changed

+209
-2
lines changed

4 files changed

+209
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Array Destructuring
2+
3+
This is an ES6 feature that allows you break a complex data structure like array into a smaller data structure like the array.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
"use strict";
2+
3+
// 🔸Destructuring Arrays🔸
4+
/*
5+
const restaurant = {
6+
name: "Classico Italiano",
7+
location: "Via Angelo, Tavanti 23, Firenze, Italy",
8+
categories: ["Italian", "Pizzeria", "Vegetarian", "Organic"],
9+
starterMenu: ["Focaccia", "Bruschetta", "Garlic Bread", "Caprese Salad"],
10+
mainMenu: ["Pizza", "Pasta", "Risotto"],
11+
12+
order: function (starterIndex, mainIndex) {
13+
return [this.starterMenu[starterIndex], this.mainMenu[mainIndex]];
14+
},
15+
};
16+
17+
const arr = [2, 3, 4];
18+
const a = arr[0];
19+
const b = arr[1];
20+
const c = arr[2];
21+
22+
const [x, y, z] = arr;
23+
console.log(x, y, z);
24+
console.log(arr);
25+
26+
let [main, , secondary] = restaurant.categories;
27+
console.log(main, secondary);
28+
29+
// Switching variables
30+
31+
// const temp = main;
32+
// main = secondary;
33+
// secondary = temp;
34+
// console.log(main, secondary);
35+
36+
[main, secondary] = [secondary, main];
37+
console.log(main, secondary);
38+
39+
// Receive 2 return values from a function
40+
const [starter, mainCourse] = restaurant.order(2, 0);
41+
console.log(starter, mainCourse);
42+
43+
const nested = [2, 4, [5, 6]];
44+
// const [i, , j] = nested;
45+
// console.log(`Test`, nested);
46+
47+
const [i, , [j, k]] = nested;
48+
console.log(i, j, k);
49+
50+
// Default values
51+
const [p = 1, q = 1, r = 1] = [8, 9];
52+
console.log(p, q, r);
53+
*/
54+
55+
// 🔸Desructuring Objects🔸
56+
const restaurant = {
57+
name: "Classico Italiano",
58+
location: "Via Angelo, Tavanti 23, Firenze, Italy",
59+
categories: ["Italian", "Pizzeria", "Vegetarian", "Organic"],
60+
starterMenu: ["Focaccia", "Bruschetta", "Garlic Bread", "Caprese Salad"],
61+
mainMenu: ["Pizza", "Pasta", "Risotto"],
62+
openingHours: {
63+
thu: {
64+
open: 12,
65+
close: 22,
66+
},
67+
fri: {
68+
open: 11,
69+
close: 23,
70+
},
71+
sat: {
72+
open: 0, // Open 24 hours
73+
close: 24,
74+
},
75+
},
76+
77+
order: function (starterIndex, mainIndex) {
78+
return [this.starterMenu[starterIndex], this.mainMenu[mainIndex]];
79+
},
80+
81+
// Destructuring objects within methods (lots of parameters)
82+
83+
orderDelivery: function ({
84+
starterIndex = 0,
85+
mainIndex = 1,
86+
time = "19:30",
87+
address = "Dublin, England",
88+
}) {
89+
console.log(
90+
`Order received ${this.starterMenu[starterIndex]} and ${this.mainMenu[mainIndex]} will be delivered to ${address} at ${time}`
91+
);
92+
},
93+
94+
orderPasta: function (ing1, ing2, ing3) {
95+
console.log(`Here is yout declicious pasta with ${ing1}, ${ing2}, ${ing3}`);
96+
},
97+
};
98+
99+
// Calling orderDelivery method with new object parameters
100+
restaurant.orderDelivery({
101+
time: "22:30",
102+
address: "Via del Sole, 21",
103+
mainIndex: 2,
104+
starterIndex: 2,
105+
});
106+
107+
// Calling orderDelivery method with only two parameters, the missing parameters will be gotten from the default values
108+
restaurant.orderDelivery({
109+
address: "Via del Sole, 21",
110+
starterIndex: 1,
111+
});
112+
113+
const { name, openingHours, categories } = restaurant;
114+
console.log(name, openingHours, categories);
115+
116+
// Changing object names using variables.
117+
const {
118+
name: restaurantName,
119+
openingHours: hours,
120+
categories: tags,
121+
} = restaurant;
122+
console.log(restaurantName, hours, tags);
123+
124+
// Setting default values
125+
const { menu = [], starterMenu: starters = [] } = restaurant;
126+
console.log(menu, starters);
127+
128+
// Mutating variables in objects
129+
let a = 111;
130+
let b = 999;
131+
const obj = { a: 23, b: 7, c: 14 };
132+
133+
({ a, b } = obj);
134+
console.log(obj);
135+
136+
// Nested objects
137+
const {
138+
fri: { open: o, close: c },
139+
} = openingHours;
140+
console.log(open, close);
141+
console.log(o, c);
142+
143+
/* 🔸Spread Operators🔸
144+
The spread operator (...) is used to quickly copy all or part of an existing array or object into another array or object.
145+
*/
146+
let arr = [7, 8, 9];
147+
console.log(arr);
148+
149+
const bad = [1, 2, arr[0], arr[1], arr[2]];
150+
console.log(bad);
151+
152+
// Copy the arr array and add it to the good array
153+
const good = [1, 2, ...arr];
154+
console.log(good);
155+
156+
// Also used for passing arguments into functions
157+
console.log(1, 2, 7, 8, 9); // ⛔
158+
console.log(...good); // ✅
159+
160+
const newMenu = [...restaurant.mainMenu, "Gnocci"];
161+
console.log(newMenu);
162+
163+
/*
164+
? Difference btw spread operator and destructuring is that spread operators does not create new variables. It can only be used in places where values are seperated by commas.
165+
*/
166+
167+
// Copy arrays
168+
const mainMenuCopy = [...restaurant.mainMenu];
169+
170+
// Join 2 arrays
171+
const menus = [...restaurant.mainMenu, ...restaurant.starterMenu];
172+
console.log(menus);
173+
174+
// Iterables: arrays, strings, maps, sets, NOT objects
175+
176+
const str = "Eke";
177+
const letters = [...str, "", "S"];
178+
console.log(letters);
179+
180+
// Spread operators can only be used when building an array or when values are passed into a funciton.
181+
// console.log(${...str} 'Victor'); Will not work
182+
183+
// Real-world example
184+
// const ingredients = [
185+
// prompt("Let's make pasta! Ingredient 1?"),
186+
// prompt("Let's make pasta! Ingredient 2?"),
187+
// prompt("Let's make pasta! Ingredient 3?"),
188+
// ];
189+
// console.log(ingredients);
190+
191+
// restaurant.orderPasta(ingredients[0], ingredients[1], ingredients[2]);
192+
193+
// restaurant.orderPasta(...ingredients);
194+
195+
// Objects
196+
const newRestaurant = { foundedIn: 1988, ...restaurant, founder: "Guiseppe" };
197+
console.log(newRestaurant);
198+
199+
const restaurantCopy = { ...restaurant };
200+
restaurantCopy.name = "Ristorante Roma";
201+
console.log(restaurantCopy.name, restaurant.name);

index.html

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
<html>
33
<head>
44
<link rel="stylesheet" href="css/main.css" />
5-
<link rel="favicon" href="favicon.ico" />
5+
<link
6+
rel="favicon"
7+
href="09-Data-Structure-Modern-Operators-and-Strings/favicon.ico"
8+
/>
69
<title>JavaScript Course</title>
710
</head>
811
<body>
912
<h1>Console.log()</h1>
10-
<script src="08-behind-the-scenes/script.js"></script>
13+
<script src="09-Data-Structure-Modern-Operators-and-Strings/script.js"></script>
1114
</body>
1215
</html>

0 commit comments

Comments
 (0)