Advertisement
samimwebdev

Loop and array helper method

Feb 28th, 2022
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //string
  2. //array loop
  3. //array
  4. //most used
  5. //Most looping system
  6. //object
  7. //for in
  8. //--------array convertion (array loop)
  9.  
  10. //for
  11. //while loop
  12. //forEach
  13.  
  14. //map
  15. // return array
  16. // work on each element
  17. //don't mutate original array
  18. //must be returned from inside
  19.  
  20. // const arrayMultiply = nums.map(num => num * 2)
  21.  
  22. // console.log(arrayMultiply)
  23.  
  24. //find
  25. // return single element
  26. // work on each element
  27. //don't mutate original array
  28. //must be returned from inside
  29.  
  30. // const result = nums.find(num => num % 2 !== 0 )
  31. // console.log(result)
  32.  
  33. // const nums = [1, 3, 5, 7, 10]
  34.  
  35. // filter
  36. // return array [element] based on condition
  37. // work on each element
  38. // don't mutate original array
  39. // must be returned from inside
  40. // const result = nums.filter(num => num % 2 == 0)
  41. // console.log(result)
  42.  
  43. // const nums = [ 1,3, 5, 7]
  44.  
  45. //some
  46. // return true/false based on condition
  47. // work on each element
  48. //don't mutate original array
  49. //must be returned from inside
  50. // const result = nums.some(num => num % 2 == 0)
  51. // console.log(result)
  52.  
  53. //const nums = [1, 3, 5, 7]
  54.  
  55. //every
  56. // return true/false based on condition
  57. // work on each element
  58. //don't mutate original array
  59. //must be returned from inside
  60. // const result = nums.every(num => num % 2 !== 0)
  61. // console.log(result)
  62.  
  63. // const nums = [0, 3, 5, 7]
  64.  
  65. // findIndex
  66. // return index number based on condition
  67. // work on each element
  68. // don't mutate original array
  69. // must be returned from inside
  70. // const result = nums.findIndex(num => num % 2 !== 0)
  71. // console.log(result)
  72.  
  73. // reduce(reducing array element)
  74. // return based on criteria
  75. // work on each element
  76. // don't mutate original array
  77. // must be returned from inside
  78. // const nums = [0, 3, 5, 7]
  79.  
  80. // acc - 0  curr-3 result - 3
  81. // acc - 3  curr-5 result - 8
  82. // acc - 8  curr-7 result - 15
  83.  
  84. // with default value
  85. // acc - 5  curr-0 result - 5
  86. // acc - 5  curr-3 result - 8
  87. // acc - 8  curr-5 result - 13
  88. // acc - 13  curr-7 result -20
  89.  
  90. // const result = nums.reduce((acc, curr, index, array) => acc + curr, 5)
  91. // console.log(result)
  92.  
  93. // const nums = {
  94. //   value1: 1,
  95. //   value2: 2,
  96. //   value3: 3
  97. // }
  98.  
  99. // console.log(Object.keys(nums))
  100. // console.log(Object.entries(nums))
  101. // let total = 0
  102. // //object looping
  103. // for (let [key, value] of Object.entries(nums)) {
  104. //   // total = total + value
  105. //   //console.log(num[1])
  106. //   // console.log(nums[num])
  107. // }
  108. // console.log(total)
  109.  
  110. // function summation(){
  111. //   //for of
  112. //   let total = 4
  113. //   for (let num of nums){
  114. //     total += num
  115. //   }
  116. //   return total
  117.  
  118. // }
  119.  
  120. //console.log(summation())
  121.  
  122. //for in(object, array-not recommended)
  123.  
  124. //array helper - map, reduce, find, findIndex, filter, some , every
  125.  
  126. // let kvArray = [{ key: 1, value: 10 },
  127. // { key: 2, value: 20 },
  128. // { key: 3, value: 30 }]
  129.  
  130. // let reformattedArray = kvArray.map(obj => {
  131. //   let rObj = {}
  132. //   rObj[obj.key] = obj.value
  133. //   return rObj
  134. // })
  135. // reformattedArray is now [{1: 10}, {2: 20}, {3: 30}],
  136.  
  137. // let people = [
  138. //   { name: 'Alice', age: 21 },
  139. //   { name: 'Max', age: 20 },
  140. //   { name: 'Jane', age: 20 }
  141. // ];
  142.  
  143. // function groupBy(objectArray, property) {
  144. //   return objectArray.reduce(function (acc, obj) {
  145. //     let key = obj[property]
  146. //     if (!acc[key]) {
  147. //       acc[key] = []
  148. //     }
  149. //     acc[key].push(obj)
  150. //     return acc
  151. //   }, {})
  152. // }
  153.  
  154. // let groupedPeople = groupBy(people, 'age')
  155. // groupedPeople is:
  156. // {
  157. //   20: [
  158. //     { name: 'Max', age: 20 },
  159. //     { name: 'Jane', age: 20 }
  160. //   ],
  161. //   21: [{ name: 'Alice', age: 21 }]
  162. // }
  163.  
  164. // let names = ['Alice', 'Bob', 'Tiff', 'Bruce', 'Alice']
  165.  
  166. // let countedNames = names.reduce(function (allNames, name) {
  167. //   if (name in allNames) {
  168. //     allNames[name]++
  169. //   }
  170. //   else {
  171. //     allNames[name] = 1
  172. //   }
  173. //   return allNames
  174. // }, {})
  175. // countedNames is:
  176. // { 'Alice': 2, 'Bob': 1, 'Tiff': 1, 'Bruce': 1 }
  177.  
  178. // let myArray = ['a', 'b', 'a', 'b', 'c', 'e', 'e', 'c', 'd', 'd', 'd', 'd']
  179. // let myOrderedArray = myArray.reduce(function (accumulator, currentValue) {
  180. //   if (accumulator.indexOf(currentValue) === -1) {
  181. //     accumulator.push(currentValue)
  182. //   }
  183. //   return accumulator
  184. // }, [])
  185.  
  186. // console.log(myOrderedArray)
  187.  
  188. //0 +1 = 1
  189. //1 + 2 = 3
  190. //3 + 3 = 6
  191. // let sum = [{ x: 1 }, { x: 2 }, { x: 3 }].reduce(function (accumulator, currentValue) {
  192. //   console.log(accumulator, currentValue)
  193. //   return accumulator.x + currentValue.x
  194. // })
  195.  
  196. // console.log(sum)
  197.  
  198. // let names = ['Alice', 'Bob', 'Tiff', 'Bruce', 'Alice']
  199.  
  200. // let countedNames = names.reduce(function (allNames, name) {
  201. //   console.log(allNames, name)
  202. //   if(allNames[name]){
  203. //     allNames[name] ++
  204. //   }else{
  205. //     allNames[name] = 1
  206. //   }
  207. //   return allNames
  208. // }, {})
  209.  
  210. // console.log(countedNames)
  211.  
  212. // // { 'Alice': 2, 'Bob': 1, 'Tiff': 1, 'Bruce': 1 }
  213.  
  214. // const  obj = {
  215. //   alice: 1,
  216. //   alice: 1
  217. // }
  218.  
  219. // console.log('alice' in obj )
  220.  
  221. let friends = [
  222.   {
  223.     name: 'Anna',
  224.     books: ['Bible', 'Harry Potter'],
  225.     age: 21
  226.   },
  227.   {
  228.     name: 'Bob',
  229.     books: ['War and peace', 'Romeo and Juliet'],
  230.     age: 26
  231.   },
  232.   {
  233.     name: 'Alice',
  234.     books: ['The Lord of the Rings', 'The Shining'],
  235.     age: 18
  236.   }
  237. ]
  238.  
  239. // allbooks - list which will contain all friends' books +
  240. // additional list contained in initialValue
  241. let allbooks = friends.reduce(function (accumulator, currentValue) {
  242.   console.log(accumulator, currentValue)
  243.   return [...accumulator, ...currentValue.books]
  244. }, [])
  245.  
  246. console.log(allbooks)
  247.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement