Advertisement
samimwebdev

class-5(rest, spread, destructuring)

Dec 11th, 2021
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //loop (way of looping)
  2. //function (default parameter)
  3. //Destructing, spreading
  4. //nested object, array access
  5.  
  6. //Loop(Repeating things)
  7. // for (let i = 0; i < 10; i++) {
  8. //   let occurrence
  9. //   if (i <= 1) {
  10. //     occurrence = 'time'
  11. //   } else {
  12. //     occurrence = 'times'
  13. //   }
  14. //   console.log(`I Love Bangladesh ${i} ${occurrence}`)
  15. // }
  16.  
  17. //Looping array and get the sum
  18. // function sum(arr) {
  19. //   let sum = 0
  20. //   for (let i = 0; i < arr.length; i++) {
  21. //     sum = sum + arr[i]
  22. //   }
  23. //   console.log(sum)
  24. // }
  25.  
  26. // sum([1, 2, 3, 4])
  27.  
  28.  
  29. //Extra (passing regular value and looping up t the point)
  30. // function sum(num) {
  31. //   let sum = 0
  32. //   for (let i = 0; i < num; i++) {
  33. //     console.log(i)
  34. //     sum += i
  35. //     // sum = sum + arr[i]
  36. //   }
  37. //   console.log(sum)
  38. // }
  39.  
  40. // sum(4)
  41.  
  42.  
  43.  
  44. //Looping array
  45. // const arr = [1, 2, 3, 4]
  46.  
  47. // for(let i = 0; i < arr.length; i++){
  48. //   console.log(i)
  49. //   console.log(arr[i])
  50. // }
  51.  
  52.  
  53. //automatic semicolon insertion(Interview Question) and Odd behavior
  54. //default parameter
  55.  
  56. // function showInfo(name = 'samim', random, country = 'Bangladesh') {
  57. //   return `I am ${name} and I Love ${country}`
  58. // }
  59.  
  60. // console.log(showInfo('Hasan', 'India', 'Pakistan'))
  61.  
  62.  
  63. //More common and generalized loop
  64. //simple for loop
  65. //while loop, do while loop
  66.  
  67. //array specific
  68. //forEach
  69. //for of
  70. //for in (NotRecommended- No order)
  71. //array helper method(map, reduce, ...)
  72.  
  73. //object specific
  74. //for in
  75.  
  76. //forEach loop
  77. // const arr = [1, 2, 3, 4]
  78. // arr.forEach(num => {
  79. //   console.log(num)
  80. // })
  81.  
  82. //for of loop
  83. // for(let num of arr){
  84. //   console.log(num)
  85. // }
  86.  
  87.  
  88.  
  89. // const obj = {
  90. //   firstName: 'samim',
  91. //   lastName: 'Hasan'
  92. // }
  93.  
  94. //Looping object(for in)
  95. // for(let prop in obj ){
  96. // console.log(obj[prop])
  97. // //  console.log(prop)
  98. // }
  99.  
  100.  
  101. //Applying array looping technique with object using keys() values() entries()
  102.  
  103. // console.log(Object.entries(obj))
  104. // for (let prop of Object.entries(obj)) {
  105. //   console.log(prop[1])
  106. // }
  107.  
  108. // for (let prop in obj.keys()) {
  109. //   console.log(obj[prop])
  110. //   //  console.log(prop)
  111. // }
  112.  
  113.  
  114.  
  115. // const arr = [1, 2, 3, 4]
  116.  
  117. // const num1 = arr[0]
  118. // const num2 = arr[1]
  119. // const num3 = arr[2]
  120. // const num4 = arr[3]
  121.  
  122. const arr = [1, 2, 3, 4]
  123. //destructuring
  124. // const[num1, , num3, num4] = arr
  125.  
  126. //rest operator
  127. const [num1, ...restArr] = arr
  128.  
  129. console.log(num1, restArr)
  130.  
  131.  
  132.  
  133. const obj = {
  134.   firstName: 'samim',
  135.   lastName: 'Hasan',
  136.   age: 27
  137. }
  138.  
  139.  
  140. //Object destructuring and rest operator
  141. const {lastName:lName = 'blabla' , ...restObj} = obj
  142. console.log(restObj)
  143. // console.log(fName, lName)
  144.  
  145.  
  146. // const arr = [1, 2, 3, 4]
  147.  
  148. //spreading arry
  149. // const newArr = [...arr, 6]
  150. // console.log(newArr)
  151.  
  152.  
  153. //spreading object
  154. const copiedObj = {
  155.   ...obj,
  156.   profession: 'web developer'
  157. }
  158.  
  159. console.log(copiedObj)
  160.  
  161.  
  162.  
  163. //remember(!!) Extra [] in using rest values as function parameter
  164. // Little useful technique
  165. // function showProfile({firstName, ... rest}){
  166. //   console.log(firstName, rest)
  167. // }
  168.  
  169. // showProfile({
  170. //   firstName: 'samim',
  171. //   lastName: 'Hasan',
  172. //   age: 27
  173. // })
  174.  
  175.  
  176.  
  177. //nested array and object value accessing
  178. const obj1 = {
  179.   obj2: {
  180.     obj3: {
  181.       hi: 'Hello',
  182.       nums: [1, 2, 3]
  183.     }
  184.   }
  185. }
  186. console.log(obj1.obj2.obj3.nums[2])
  187.  
  188. const arr1 = [[1, 3, [10, 12], 4], 6, [9, 10]]
  189. console.log(arr1[0][2][1])
  190.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement