const weekdays = ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun'];
const openingHours = {
[weekdays[3]]: {
open: 12,
close: 22,
},
[weekdays[4]]: {
open: 11,
close: 23,
},
[weekdays[5]]: {
open: 0, // Open 24 hours
close: 24,
},
};
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'],
// ES6 enhanced object literals
openingHours,
order(starterIndex, mainIndex) {
return [this.starterMenu[starterIndex], this.mainMenu[mainIndex]];
},
orderDelivery({ starterIndex = 1, mainIndex = 0, time = '20:00', address }) {
console.log(
`Order received! ${this.starterMenu[starterIndex]} and ${this.mainMenu[mainIndex]} will be delivered to ${address} at ${time}`
);
},
orderPasta(ing1, ing2, ing3) {
console.log(
`Here is your declicious pasta with ${ing1}, ${ing2} and ${ing3}`
);
},
orderPizza(mainIngredient, ...otherIngredients) {
console.log(mainIngredient);
console.log(otherIngredients);
},
};
// Destructuring Arrays
const arr = [2, 3, 4];
const a = arr[0];
const b = arr[1];
const c = arr[2];
const [x, y, z] = arr;
console.log(x, y, z);
console.log(arr);
let [main, , secondary] = restaurant.categories;
console.log(main, secondary);
// Switching variables
// const temp = main;
// main = secondary;
// secondary = temp;
// console.log(main, secondary);
[main, secondary] = [secondary, main];
console.log(main, secondary);
// Receive 2 return values from a function
const [starter, mainCourse] = restaurant.order(2, 0);
console.log(starter, mainCourse);
// Nested destructuring
const nested = [2, 4, [5, 6]];
// const [i, , j] = nested;
const [i, , [j, k]] = nested;
console.log(i, j, k);
// Default values
const [p = 1, q = 1, r = 1] = [8, 9];
console.log(p, q, r); ..... // cl: 8, 9, 1
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'],
openingHours : {
thu : {
open : 12,
close : 22,
},
fri : {
open : 11,
close : 23,
},
sat : {
open : 0,
close : 24,
},
},
order(starterIndex, mainIndex) {
return [this.starterMenu[starterIndex], this.mainMenu[mainIndex]];
},
orderDelivery({ starterIndex = 1, mainIndex = 0, time = '20:00', address }) {
console.log(
`Order received! ${this.starterMenu[starterIndex]} and ${this.mainMenu[mainIndex]} will be delivered to ${address} at ${time}`
);
},
};
// Destructuring Objects
restaurant.orderDelivery({
time: '22:30',
address: 'Via del Sole, 21',
mainIndex: 2,
starterIndex: 2,
});
restaurant.orderDelivery({
address: 'Via del Sole, 21',
starterIndex: 1,
});
const { name, openingHours, categories } = restaurant;
console.log(name, openingHours, categories);
const {
name: restaurantName,
openingHours: hours,
categories: tags,
} = restaurant;
console.log(restaurantName, hours, tags);
// Default values
const { menu = [], starterMenu: starters = [] } = restaurant; // default value
console.log(menu, starters);
// Mutating variables
let a = 111;
let b = 999;
const obj = { a: 23, b: 7, c: 14 };
({ a, b } = obj); // you need code block
console.log(a, b); // cl: 23 7
// Nested objects
const {
fri: { open: o, close: c },
} = openingHours;
console.log(o, c);
// The Spread Operator (...)
const arr = [7, 8, 9];
const badNewArr = [1, 2, arr[0], arr[1], arr[2]];
console.log(badNewArr);
const newArr = [1, 2, ...arr];
// arr의 모든 요소를 꺼내 individual하게 저장함
console.log(newArr);
console.log(...newArr);
console.log(1, 2, 7, 8, 9);
const newMenu = [...restaurant.mainMenu, 'Gnocci'];
console.log(newMenu);
// Copy array
const mainMenuCopy = [...restaurant.mainMenu];
// Join 2 arrays
const menu = [...restaurant.starterMenu, ...restaurant.mainMenu];
console.log(menu);
// Iterables: arrays, strings, maps, sets. NOT objects
const str = 'Jonas';
const letters = [...str, ' ', 'S.'];
console.log(letters);
console.log(...str);
// console.log(`${...str} Schmedtmann`);
// Real-world example
const ingredients = [
// prompt("Let's make pasta! Ingredient 1?"),
// prompt('Ingredient 2?'),
// prompt('Ingredient 3'),
];
console.log(ingredients);
restaurant.orderPasta(ingredients[0], ingredients[1], ingredients[2]);
restaurant.orderPasta(...ingredients);
// Objects
const newRestaurant = { foundedIn: 1998, ...restaurant, founder: 'Guiseppe' };
console.log(newRestaurant);
const restaurantCopy = { ...restaurant };
restaurantCopy.name = 'Ristorante Roma';
console.log(restaurantCopy.name);
console.log(restaurant.name);
....
orderPizza(mainIngredient, ...otherIngredients) {
console.log(mainIngredient);
console.log(otherIngredients);
},
.....
// Rest Pattern and Parameters
// 1) Destructuring
// SPREAD, because on RIGHT side of =
const arr = [1, 2, ...[3, 4]];
// REST, because on LEFT side of =
const [a, b, ...others] = [1, 2, 3, 4, 5];
console.log(a, b, others); // cl : 1 2 [3, 4, 5]
const [pizza, , risotto, ...otherFood] = [
...restaurant.mainMenu,
...restaurant.starterMenu,
];
console.log(pizza, risotto, otherFood);
// cl : pizza risotto ["Focaccia", "Bruschetta", "Garlic Bread", "Caprese Salad"]
// Objects
const { sat, ...weekdays } = restaurant.openingHours;
console.log(weekdays);
// 2) Functions
const add = function (...numbers) {
let sum = 0;
for (let i = 0; i < numbers.length; i++) sum += numbers[i];
console.log(sum);
};
add(2, 3);
add(5, 3, 7, 2);
add(8, 2, 5, 3, 2, 1, 4);
const x = [23, 5, 7];
add(...x);
restaurant.orderPizza('mushrooms', 'onion', 'olives', 'spinach');
restaurant.orderPizza('mushrooms');
// Short Circuiting (&& and ||)
console.log('---- OR ----');
// Use ANY data type, return ANY data type, short-circuiting
console.log(3 || 'Jonas');
// cl : 3
// short circuiting means that if the first value is a truthy value, it'll immediately return the first value. In this case(||), It doesnt' check the next one 'Jonas'. That is why it is "Short" Circuiting.
console.log('' || 'Jonas');
// cl: 'Jonas'
console.log(true || 0);
// cl : true
console.log(undefined || null);
// cl : null
// undefined is a falsy value, and so we then go to the second operand. so there is no short-circuiting, and so then 'null' will be returned (even though null is also a falsy value).
console.log(undefined || 0 || '' || 'Hello' || 23 || null);
// cl : 'Hello'
restaurant.numGuests = 0;
const guests1 = restaurant.numGuests ? restaurant.numGuests : 10;
console.log(guests1);
// cl : 10
const guests2 = restaurant.numGuests || 10;
console.log(guests2);
// cl : 10
// 하지만 restaurant.numGuests가 23이라면 23이 출력될 것. falsy하지 않으니까.
console.log('---- AND ----');
console.log(0 && 'Jonas');
// cl : 0
console.log(7 && 'Jonas');
// cl : 'Jonas'
// In this case(&&), it returns the last value.
// &&의 경우에는 falsy가 하나만 있어도 falsy하기 때문에 아이템을 끝까지 탐색해야하므로 last one을 반환하게 된다. ||의 경우에는 truthy가 하나만 나와도 truthy하므로 첫 번째 truthy item을 보자마자 탐색이 종료되기에 first truthy one이 반환되는 것이다.
console.log('Hello' && 23 && null && 'jonas');
// cl : null
// Practical example
if (restaurant.orderPizza) {
restaurant.orderPizza('mushrooms', 'spinach');
} // orderPizza가 존재해야 if문을 execute
restaurant.orderPizza && restaurant.orderPizza('mushrooms', 'spinach');
// 이것 역시 restaurant.orderPizza가 not existing(undefined)하면 아무 일도 벌어지지 않기 때문에 위의 if문과 같은 기능을 수행하게 된다.
// 그렇다고 이렇게 다 작성하지는 마라. 나중에 readable하지 않을 수 있다.
// The Nullish Coalescing Operator
restaurant.numGuests = 0;
const guests = restaurant.numGuests || 10;
console.log(guests); // cl : 0
// Nullish: null and undefined (NOT 0 or '')
restaurant.numGuests = 1;
const guestCorrect = restaurant.numGuests ?? 10;
console.log(guestCorrect); // cl : 10
// Logical Assignment Operators
const rest1 = {
name: 'Capri',
// numGuests: 20,
numGuests: 0,
};
const rest2 = {
name: 'La Piazza',
owner: 'Giovanni Rossi',
};
// OR assignment operator
// rest1.numGuests = rest1.numGuests || 10;
// rest2.numGuests = rest2.numGuests || 10;
// rest1.numGuests ||= 10;
// rest2.numGuests ||= 10;
// nullish assignment operator (null or undefined)
rest1.numGuests ??= 10;
rest2.numGuests ??= 10;
// AND assignment operator
// rest1.owner = rest1.owner && '<ANONYMOUS>';
// rest2.owner = rest2.owner && '<ANONYMOUS>';
rest1.owner &&= '<ANONYMOUS>';
rest2.owner &&= '<ANONYMOUS>';
console.log(rest1);
// cl : {name : 'Capri', numGuests: 0}
console.log(rest2);
// cl : {name : 'La Pizza', owner : '<ANONYMOUS>', numGuests : 10}
// The for-of Loop
const menu = [...restaurant.starterMenu, ...restaurant.mainMenu];
for (const item of menu) console.log(item);
for (const item of menu.entries()) console.log(item);
for (const [i, el] of menu.entries()) {
console.log(`${i + 1}: ${el}`);
}
/* cl :
1 : Focaccia
2 : Burschetta
....
*/
// console.log([...menu.entries()]);
const weekdays = ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun'];
const openingHours = {
[weekdays[3]]: {
open: 12,
close: 22,
},
[weekdays[4]]: {
open: 11,
close: 23,
},
[weekdays[5]]: {
open: 0, // Open 24 hours
close: 24,
},
};
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'],
// ES6 enhanced object literals
openingHours,
order(starterIndex, mainIndex) {
return [this.starterMenu[starterIndex], this.mainMenu[mainIndex]];
},
orderDelivery({ starterIndex = 1, mainIndex = 0, time = '20:00', address }) {
console.log(
`Order received! ${this.starterMenu[starterIndex]} and ${this.mainMenu[mainIndex]} will be delivered to ${address} at ${time}`
);
},
orderPasta(ing1, ing2, ing3) {
console.log(
`Here is your declicious pasta with ${ing1}, ${ing2} and ${ing3}`
);
},
orderPizza(mainIngredient, ...otherIngredients) {
console.log(mainIngredient);
console.log(otherIngredients);
},
};
// Optional Chaining
if (restaurant.openingHours && restaurant.openingHours.mon)
console.log(restaurant.openingHours.mon.open);
// console.log(restaurant.openingHours.mon.open); 에러남
// WITH optional chaining
console.log(restaurant.openingHours.mon?.open);
// cl : undefined
console.log(restaurant.openingHours?.mon?.open);
// Example
const days = ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun'];
for (const day of days) {
const open = restaurant.openingHours[day]?.open ?? 'closed';
console.log(`On ${day}, we open at ${open}`);
}
// Methods
console.log(restaurant.order?.(0, 1) ?? 'Method does not exist');
console.log(restaurant.orderRisotto?.(0, 1) ?? 'Method does not exist');
// Arrays
const users = [{ name: 'Jonas', email: 'hello@jonas.io' }];
// const users = [];
console.log(users[0]?.name ?? 'User array empty');
if (users.length > 0) console.log(users[0].name);
else console.log('user array empty');
// Looping Objects: Object Keys, Values, and Entries
// Property NAMES
const properties = Object.keys(openingHours);
console.log(properties);
// cl : ["thu", "fri", "sat"]
for (const day of properties) console.log(day);
// cl : thu fri sat // 줄바꿈해서
let openStr = `We are open on ${properties.length} days: `; // cl : we are open on 3 days :
for (const day of properties) {
openStr += `${day}, `;
}
console.log(openStr);
// cl : we are open on 3 days : thu, fri, sat
// Property VALUES
const values = Object.values(openingHours);
console.log(values);
// Entire object
const entries = Object.entries(openingHours);
// console.log(entries);
// cl : 0 : {open : 12, close: 22} ...
// [key, value]
for (const [day, { open, close }] of entries) {
console.log(`On ${day} we open at ${open} and close at ${close}`);
}
// Sets
const ordersSet = new Set([
'Pasta',
'Pizza',
'Pizza',
'Risotto',
'Pasta',
'Pizza',
]); // having duplicate stuffs
console.log(ordersSet);
// cl : Set(3) {"Pasta", "Pizza", "Risotto"}
// Set is iterable
console.log(new Set('Jonas'));
// cl : Set(5) {"J", ... , "s"}
console.log(ordersSet.size); // 3
console.log(ordersSet.has('Pizza')); // true
console.log(ordersSet.has('Bread')); // false
ordersSet.add('Garlic Bread');
ordersSet.add('Garlic Bread');
ordersSet.delete('Risotto');
// ordersSet.clear();
console.log(ordersSet);
// cl: Set(3) {"Pasta", "Pizza", "Garlic Bread"}
for (const order of ordersSet) console.log(order);
// Example
const staff = ['Waiter', 'Chef', 'Waiter', 'Manager', 'Chef', 'Waiter'];
const staffUnique = [...new Set(staff)];
console.log(staffUnique);
console.log(
new Set(['Waiter', 'Chef', 'Waiter', 'Manager', 'Chef', 'Waiter']).size
);
console.log(new Set('jonasschmedtmann').size);
*/
// Maps: Fundamentals
const rest = new Map();
rest.set('name', 'Classico Italiano');
rest.set(1, 'Firenze, Italy');
console.log(rest.set(2, 'Lisbon, Portugal'));
// cl : Map(3) {"name"=>"Classico Italiano", 1=> "Firenze, Italy", 2=>"Lisbon, Portugal"}
// set 함수는 updated 된 map을 반환
rest
.set('categories', ['Italian', 'Pizzeria', 'Vegetarian', 'Organic'])
.set('open', 11)
.set('close', 23)
.set(true, 'We are open :D')
.set(false, 'We are closed :(');
// set 함수를 이렇게 바꿔서 사용할 수도 있다
console.log(rest.get('name'));
// cl : Classico Italiano
console.log(rest.get(true));
// cl : we are open :D
console.log(rest.get(1));
// cl : undefined
let time = 21;
console.log(rest.get(time > rest.get('open') && time < rest.get('close')));
// cl : We are open :D
time = 8;
console.log(rest.get(time > rest.get('open') && time < rest.get('close')));
// cl : We are close :(
console.log(rest.has('categories'));
rest.delete(2);
// rest.clear();
const arr = [1, 2];
rest.set(arr, 'Test');
rest.set(document.querySelector('h1'), 'Heading');
console.log(rest);
console.log(rest.size);
console.log(rest.get(arr));
// Maps: Iteration
const question = new Map([
['question', 'What is the best programming language in the world?'],
[1, 'C'],
[2, 'Java'],
[3, 'JavaScript'],
['correct', 3],
[true, 'Correct 🎉'],
[false, 'Try again!'],
]);
console.log(question);
// Convert object to map
console.log(Object.entries(openingHours));
const hoursMap = new Map(Object.entries(openingHours));
console.log(hoursMap);
// Quiz app
console.log(question.get('question'));
for (const [key, value] of question) {
if (typeof key === 'number') console.log(`Answer ${key}: ${value}`);
}
// const answer = Number(prompt('Your answer'));
const answer = 3;
console.log(answer);
console.log(question.get(question.get('correct') === answer));
// Convert map to array
console.log([...question]);
// console.log(question.entries());
console.log([...question.keys()]);
console.log([...question.values()]);
Sources of Data
1. From the program itself : Data written directly in source code (e.g. status messages)
2. From the UI : Data input from the user or data written in DOM (e.g. tasks in todo app)
3. From external sources : Data fetched for example from web API (e.g. recipe objects)
=> Collection of data
=> Data Structure
if(simple list) Arrays or Sets
if(Key/Value Pairs) Objects or Maps // Keys allow us to describe value