Skip to content

Instantly share code, notes, and snippets.

@kironroy
Created May 20, 2023 01:13
Show Gist options
  • Save kironroy/6fd57ca678bccfeb7f51ff0b42ff265a to your computer and use it in GitHub Desktop.
Save kironroy/6fd57ca678bccfeb7f51ff0b42ff265a to your computer and use it in GitHub Desktop.
'use strict';
// Data needed for a later exercise
const flights =
'_Delayed_Departure;fao93766109;txl2133758440;11:25+_Arrival;bru0943384722;fao93766109;11:45+_Delayed_Arrival;hel7439299980;fao93766109;12:05+_Departure;fao93766109;lis2323639855;12:30';
// Data needed for first part of the section
const restaurant = {
name: "Vini's Curry House",
location: '123 Gora Way, Mumbai, India',
categories: ['Punjabi', 'Bengali', 'Madras', 'Neplai'],
starterMenu: ['Samosa', 'Pakora', 'Nan', 'Kebab'],
mainMenu: ['Dosa', 'Chicken Madras', 'Tandoori Halibut'],
// openingHours: {
// thu: {
// open: 12,
// close: 22,
// },
// fri: {
// open: 11,
// close: 23,
// },
// sat: {
// open: 0, // Open 24 hours
// close: 24,
// },
// },
order: function (starterIndex, mainIndex) {
return [this.starterMenu[starterIndex], this.mainMenu[mainIndex]];
},
};
// Destructuring Arrays
// Destructuring is an ES6 feature of unpacking values
// from and array or object to a variable
// not destructive
const arr = [2, 3, 4];
// old way
// const a = arr[0];
// const b = arr[1];
// const c = arr[2];
// new way (destructuring)
// destructuring works from the Right side of assignment
const [a, b, c] = arr;
console.log(a, b, c);
// take first and second categories
let [first, second] = restaurant.categories;
console.log(first, second);
// take first and third (skip second)
const [firstAgain, , third] = restaurant.categories;
console.log(firstAgain, third);
// take second and fourth (skip second)
const [, secondAgain, , fourth] = restaurant.categories;
console.log(secondAgain, fourth);
// switch main and secondary category (Bengali first, then Punjabi)
[first, second] = [second, first];
console.log(first, second); // Not permanent
// All elements with spread... oper
console.log(...restaurant.categories);
// functions multiple values
console.log(restaurant.order(2, 2));
// simply destruct: console.log(restaurant.order(2,2));
// Recieve 2 return values from a function
const [starter, main] = restaurant.order(2, 0);
console.log(starter, main);
// Nested arrays
const nestedArr = [2, 4, [5, 6]];
// const [i, , j] = nestedArr;
// console.log(i, j);
const [i, , [j, k]] = nestedArr;
console.log(i, j, k);
// Default values
// useful when working with APIs
const [p=1, q=1, r=1] = [8, 9];
console.log(p, q, r);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment