99 lines
2.5 KiB
JavaScript

const arr = [2, 4, 8, 16, 32, 64];
const badNewArr = [0, 1, arr[0], arr[1], arr[2], arr[3], arr[4], arr[5]];
const goodNewArr = [0, 1, ...arr];
console.log(badNewArr);
console.log(goodNewArr);
// output will be not [2, 4, 8, 16, 32, 64] but 2, 4, 8, 16, 32, 64
console.log(...goodNewArr);
const restauraunt = {
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"],
order: function (starterIndex, mainIndex) {
return [this.starterMenu[starterIndex], this.mainMenu[mainIndex]];
},
orderDelivery: function ({
starterIndex = 1,
mainIndex = 0,
time = "20:00",
address,
}) {
console.log(
`Order recieved! ${this.starterMenu[starterIndex]} and ${this.mainMenu[mainIndex]} will be delivered to ${address} at ${time}`
);
},
openingHours: {
thu: {
open: 12,
close: 22,
},
fri: {
open: 11,
close: 23,
},
sat: {
open: 0,
close: 24,
},
},
orderPasta: function (ing1, ing2, ing3) {
console.log(
`Here is your delicious pasta with ${ing1}, ${ing2} and ${ing3}`
);
},
orderPizza: function (mainIngridient, ...otherIngridients) {
console.log(mainIngridient);
console.log(otherIngridients);
},
};
const arr2 = [0, 1, ...[3, 4]];
const [a, b, ...others] = [1, 2, 3, 4];
console.log("REST:", a, b, others);
const [pizza, , risotto, ...otherFood] = [
...restauraunt.mainMenu,
...restauraunt.starterMenu,
];
console.log(pizza, risotto, otherFood);
const newMenu = [...restauraunt.mainMenu, "Gnocci"];
console.log(...newMenu);
const newMenuCopy = [...restauraunt.mainMenu];
const menu = [...newMenu, ...restauraunt.starterMenu];
console.log();
// Iterables: array, strings,maps,sets. But NOT Objects
const person = "David";
const letters = [...person];
//Objects with the spread operator
const { sat, ...weekDays } = restauraunt.openingHours;
console.log(sat, weekDays);
const add = function (...numbers) {
let sum = 0;
for (let i = 0; i < numbers.length; i++) {
sum += numbers[i];
}
console.log("SUM:", sum);
};
add(2, 3);
add(2, 3, 6, 4, 8, 4);
add(2, 3, 6, 4, 76, 32, 78, 8, 4);
const x = [6, 4, 7, 4];
const y = [6, 1, 4, 5, 4, 0, 7, 9, 4];
// sum of unpacked x and y arrays
add(...x, ...y); // 6, 4, 7, 4,6, 1, 4, 5, 4, 0, 7, 9, 4 = 61
restauraunt.orderPizza("muschrooms", "onion", "olives", "spinach");