Skip to content

Commit

Permalink
0.105 The Spread Operator
Browse files Browse the repository at this point in the history
  • Loading branch information
donsvederus committed May 16, 2022
1 parent 75c7455 commit f4bfc93
Showing 1 changed file with 103 additions and 44 deletions.
147 changes: 103 additions & 44 deletions script.js
Expand Up @@ -41,52 +41,111 @@ const restaurant = {
`Order received! ${this.starterMenu[starterIndex]} and ${this.mainMenu[mainIndex]} will be delivered to ${address} at ${time}`
);
},

orderPasta: function (ing1, ing2, ing3) {
console.log(
`Here is your delicious past with ${ing1}, ${ing2}, and ${ing3}`
);
},
};

restaurant.orderDelivery({
time: '22:30',
address: 'Via del Sole, 21',
mainIndex: 2,
starterIndex: 2,
});

restaurant.orderDelivery({
address: 'Via del Sole, 21',
starterIndex: 1,
});

// To destructure objects we use {}
const { name, openingHours, categories } = restaurant;
console.log(name, openingHours, categories);

const {
name: restaurantName,
openingHours: hours,
categories: tags,
} = restaurant;

console.log(restaurantName, hours, tags);

// Defualt Values
const { menu = [], starterMenu: starters = [] } = restaurant;
console.log(menu, starters);

// Mutating Variables
let a = 111;
let b = 999;
console.log(a, b);

const obj = { a: 23, b: 7, c: 14 };
console.log(a, b);

({ a, b } = obj);
console.log(a, b);

// Nested objects
const {
fri: { open: o, close: c },
} = openingHours;
console.log(o, c);
const arr = [7, 8, 9];

// old way to add elements to an array
const badNewArr = [1, 2, arr[0], arr[1], arr[2]];
console.log(badNewArr);

// new way to add elements to any array, new in E6, Spread Operator
const newArr = [1, 2, ...arr];
console.log(newArr);

// add elements to an object with Spread Operator
// note: we are creating a new array, and not mutating the origianal array
const newMenu = [...restaurant.mainMenu, 'Gnocci'];
console.log(newMenu);

// Copy Array
const mainMenuCopy = [...restaurant.mainMenu];
console.log(mainMenuCopy);

// Join 2 arrays
const menu = [...restaurant.starterMenu, ...restaurant.mainMenu];
console.log(menu);

// Iterables: arrays, strings, maps sets. Not objects
const str = 'Svederus';
const letters = [...str, ' ', 'S.'];
console.log(letters);
console.log(...str);
// console.log(`${str}` don); // wont work, multiple elements only epxected in new arrays or funtions.

// Real-world Example
const ingredients = [
// prompt("let's make pasta! Ingredient 1? "),
// prompt('Ingredient 2?'),
// prompt('Ingredient 3?'),
];
console.log(ingredients);

//old way
restaurant.orderPasta(ingredients[0], ingredients[1], ingredients[2]);

// new way
restaurant.orderPasta(...ingredients);

// Objects
const newRestaurant = { foundedIn: 1988, ...restaurant, founder: 'Guiseppe' };
console.log(newRestaurant);

const restaurantCopy = { ...restaurant };
restaurantCopy.name = 'Ristorante Roma';
console.log(restaurantCopy.name);
console.log(restaurant.name);

// restaurant.orderDelivery({
// time: '22:30',
// address: 'Via del Sole, 21',
// mainIndex: 2,
// starterIndex: 2,
// });

// restaurant.orderDelivery({
// address: 'Via del Sole, 21',
// starterIndex: 1,
// });

// // To destructure objects we use {}
// const { name, openingHours, categories } = restaurant;
// console.log(name, openingHours, categories);

// const {
// name: restaurantName,
// openingHours: hours,
// categories: tags,
// } = restaurant;

// console.log(restaurantName, hours, tags);

// // Defualt Values
// const { menu = [], starterMenu: starters = [] } = restaurant;
// console.log(menu, starters);

// // Mutating Variables
// let a = 111;
// let b = 999;
// console.log(a, b);

// const obj = { a: 23, b: 7, c: 14 };
// console.log(a, b);

// ({ a, b } = obj);
// console.log(a, b);

// // Nested objects
// const {
// fri: { open: o, close: c },
// } = openingHours;
// console.log(o, c);

///////////////////////////
//Destructuring Arrays
Expand Down

0 comments on commit f4bfc93

Please sign in to comment.