Guest User

Untitled

a guest
Nov 15th, 2017
385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.83 KB | None | 0 0
  1. // ES6 Destructuring
  2. // lots of duplicate code can be saved with destructuring
  3.  
  4.  
  5. // ES5
  6. var expense = {
  7. type: 'Business',
  8. amount: '$45 USD'
  9. };
  10.  
  11. var type = expense.type;
  12. var amount = expense.amount;
  13.  
  14.  
  15. // ES6
  16. const { type } = expense
  17. const { amount } = expense
  18.  
  19. // even shorter
  20. const { type, amount } = expense
  21. // creates expense.type and expense.amount
  22.  
  23.  
  24.  
  25.  
  26.  
  27. // Destructuring Arguments Object
  28.  
  29. var savedFile = {
  30. extension: 'jpg',
  31. name: 'repost',
  32. size: 14040
  33. };
  34.  
  35. // ES5
  36. // object represents file on hdd
  37.  
  38. function fileSummary(file) {
  39. return `The file ${file.name}.${file.extension} is of size ${file.size}`;
  40. }
  41.  
  42. fileSummary(savedFile);
  43.  
  44.  
  45. // ES6
  46. function fileSummaryES6( { name, extension, size } ) {
  47. return `The file ${name}.${extension} is of size ${size}`;
  48. }
  49.  
  50. fileSummaryES6(savedFile);
  51.  
  52. // ES6 destructuring with a second argument
  53. function fileSummaryExtES6( { name, extension, size }, { color } ) {
  54. return `The file ${name}.${extension} is of size ${size} and ${color} as color`;
  55. }
  56.  
  57. fileSummaryExtES6(savedFile, { color: 'red'});
  58.  
  59.  
  60.  
  61. // Destructuring Arrays
  62.  
  63. // destructuring an array is pulling off individual elements
  64. const companies = [
  65. 'Google',
  66. 'Facebook',
  67. 'Uber'
  68. ]
  69.  
  70. const [ name, name2, name3 ] = companies;
  71. name //returns Google
  72.  
  73.  
  74. // destructure a property with the curly braces
  75. const { length } = companies;
  76. length // returns 3
  77.  
  78. // destructure
  79. const [ firstCompany ] = companies;
  80. const [ lastCompany ] = companies;
  81. firstCompany // returns "Google"
  82. lastCompany // returns "Google"
  83.  
  84. // in combination with the rest operator
  85. const [ companyName, ...rest ] = companies
  86. companyName // returns "Google"
  87. rest // returns ["Facebook", "Uber"]
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94. // Destructuring Arrays and Objects at the same time
  95. const companies = [
  96. { name: 'Google', location: 'Mountain View'},
  97. { name: 'facebook', location: 'Menlo Park'},
  98. { name: 'Uber', location: 'San Fransisco'}
  99. ]
  100.  
  101. // ES5
  102. const location = companies[0].location;
  103.  
  104. // ES6
  105. // outside to the inside
  106. const [{ location }] = companies
  107.  
  108.  
  109.  
  110. // walking direction to destructuring
  111. const Google = {
  112. locations: ['Mountain View', 'New York', 'London']
  113. }
  114.  
  115. // look at the 'locations' property
  116. // returns array
  117. // take out array
  118. // pull out first element
  119. const { locations: [ location ] } = Google;
  120. // returns Mountain View
  121.  
  122.  
  123.  
  124.  
  125. // ARGUMENTS
  126. // good use for destructuring arguments
  127.  
  128. function signup(username, password) {
  129. // create new user
  130. }
  131.  
  132. signup('Boris','sd78aHg3')
  133.  
  134.  
  135. // later on you extend the function
  136. function signup(username, password, email, dateOfBirth, city) {
  137. // create new user
  138. }
  139.  
  140. // it is dangerous because the order of the parameters can change!
  141. // like here on purpose
  142. signup('Boris','sd78aHg3', '1/1/1983', 'boris@boo.com', 'New York')
  143.  
  144.  
  145. // instead of a list of strings
  146. // use a object
  147.  
  148. const user = {
  149. username: 'Boris',
  150. password: 'sd78aHg3',
  151. email: 'boris@boo.com',
  152. dateOfBirth: '1/1/1983',
  153. city: 'New York'
  154. }
  155.  
  156. // now the order is no problem because we destrucuted them
  157. function signup({username, password, email, dateOfBirth, city}) {
  158. // create new user
  159. }
  160.  
  161. signup(user);
  162.  
  163.  
  164.  
  165. // ARRAY
  166. // good use for destructuring arrays
  167.  
  168. const points = [
  169. [4, 5],
  170. [10, 1],
  171. [0, 40]
  172. ]
  173.  
  174. // what we want to get out of these nester arrays
  175. [
  176. { x: 4, y: 5 },
  177. { x: 10, y: 1 },
  178. { x: 0, y: 40 },
  179. ]
  180.  
  181.  
  182. // map over the list wioth all the points
  183. points.map(([ x, y ]) => {
  184.  
  185. // and return the points in objects
  186. return { x: x, y: y }
  187.  
  188. // improved object literals
  189. return { x, y }
  190. })
  191.  
  192.  
  193.  
  194.  
  195. // QUIZ ONE
  196. // The snippet of code below duplicates references to 'profile' inside of the 'isEngineer' function. Perhaps we can reduce the amount of code used for referencing the 'title' and 'department' properties. Refactor this code to use destructuring. Can you get the body of the 'isEngineer' function down to a single line?
  197.  
  198. const profile = {
  199. title: 'Engineer',
  200. department: 'Engineering'
  201. };
  202.  
  203. function isEngineer(profile) {
  204. var title = profile.title;
  205. var department = profile.department;
  206. return title === 'Engineer' && department === 'Engineering';
  207. }
  208.  
  209. // change the arguments to make the fn a one liner
  210. function isEngineer({title, department}) {
  211. return title === 'Engineer' && department === 'Engineering';
  212. }
  213.  
  214.  
  215.  
  216. // QUIZ TWO
  217. // The 'classes' variable holds an array of arrays, where each array represents a single class that a student is enrolled in. Convert this array of arrays into an array of objects, where each object has the keys 'subject', 'time', and 'teacher' and assign the result to 'classesAsObject. Use array destructuring and the map helper.
  218. // An array for a class has the form [subject, time, teacher]
  219. // The resulting data structure should look something like the following:
  220. //
  221. // const classesAsObject = [{ subject: 'Geography', time: '2PM', teacher: 'Mrs. Larsen' }]
  222.  
  223. const classes = [
  224. [ 'Chemistry', '9AM', 'Mr. Darnick' ],
  225. [ 'Physics', '10:15AM', 'Mrs. Lithun'],
  226. [ 'Math', '11:30AM', 'Mrs. Vitalis' ]
  227. ];
  228.  
  229. const classesAsObject;
  230.  
  231. // es6 map destructor
  232. const classesAsObject = classes.map(([subject, time, teacher]) => {
  233. return { subject, time, teacher }
  234. });
  235.  
  236.  
  237.  
  238. // QUIZ THREE Recursion with Destructuring
  239. // Use array destructuring, recursion, and the rest/spread operators to create a function 'double' that will return a new array with all values inside of it multiplied by two. Do not use any array helpers! Sure, the map, forEach, or reduce helpers would make this extremely easy but give it a shot the hard way anyways :)
  240. // Input:
  241. // double([1,2,3])
  242. //
  243. // Output
  244. // [2,4,6]
  245. //
  246. // Hint: Don't forget that with recursion you must add a base case so you don't get an infinite call stack. For example, if 'const [ number, ...rest ] = numbers' and number is undefined do you need to keep walking through the array?
  247.  
  248. const numbers = [1, 2, 3];
  249.  
  250. // solution 1
  251. const double = ([num, ...rest]) =>
  252. rest.length?
  253. [ num *2, ...double(rest) ] : [ num * 2 ];
  254.  
  255. double(numbers);
  256.  
  257. // solution 2
  258. const doubledNumbers = numbers.map(number => {
  259. return number * 2;
  260. });
Add Comment
Please, Sign In to add comment