Advertisement
samimwebdev

6.Array Helper methods

Jun 6th, 2022
184
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Array helper method
  2. //finding a item from array
  3. // looping array
  4.  
  5. //find
  6. //filter
  7. //every
  8. //some
  9. //findIndex
  10. //map
  11. //reduce
  12.  
  13. //works on array
  14.  
  15. //find
  16. //must working on array
  17. // must return something to get the result
  18. //results a (single) the matched value based on condition
  19. // const arr = [1, 3, 5, 7]
  20.  
  21. // const result = arr.find((elm) => elm === 10)
  22.  
  23. // console.log(result)
  24.  
  25. //filter
  26. //must working on array
  27. // must return something to get the result
  28. //results a (single) the matched value based on condition
  29. // const arr = [1, 3, 5, 7]
  30.  
  31. // const result = arr.filter((elm) => elm < 3)
  32.  
  33. // console.log(result)
  34.  
  35. //must working on array
  36. // must return something to get the result
  37. //results the index (single) matched value based on condition
  38. // const arr = [1, 3, 5, 7]
  39.  
  40. // const result = arr.findIndex((elm) => elm === 3)
  41.  
  42. // console.log(result)
  43.  
  44. //must working on array
  45. // must return something to get the result
  46. //results an array having exact number of element(original source)
  47. // const arr = [1, 3, 5, 7]
  48.  
  49. // const result = arr.map((elm) => elm * 3 )
  50.  
  51. // console.log(result)
  52.  
  53. //every
  54. //must working on array
  55. // must return something to get the result
  56. //results an true or false based on condition
  57. // const arr = [1, 3, 5, 7]
  58.  
  59. // const result = arr.every((elm) => elm > 0 )
  60.  
  61. // console.log(result)
  62.  
  63. //some
  64. //must working on array
  65. // must return something to get the result
  66. //results an true or false based on condition
  67. // const arr = [1, 3, 5, 7]
  68.  
  69. // const result = arr.some((elm) => elm > 10 )
  70.  
  71. // console.log(result)
  72.  
  73. //reduce
  74. //must working on array
  75. // must return something to get the result
  76. //results depends what you want to do!
  77.  
  78. // 1 + 3 + 5 + 7
  79. // const arr = [1, 3, 5, 7]
  80. // // acc = 1, curr = 3  sum = 4
  81. // // acc = 4 curr = 5 sum = 9
  82. // // acc = 9 curr = 7 sum = 16
  83.  
  84. // const result = arr.reduce(
  85. //   (acc, curr) => {
  86. //     console.log(acc, curr)
  87. //     return {
  88. //       sum: acc.sum + curr,
  89. //     }
  90. //   },
  91. //   {
  92. //     sum: 10,
  93. //   }
  94. // )
  95.  
  96. // console.log(result)
  97.  
  98. // const arr = ['samim', 'Hasan']
  99.  
  100. // const result = arr.reduce((prevVal, currentVal) => {
  101. //   return prevVal + ' ' + currentVal
  102. // }, '')
  103.  
  104. // console.log(result)
  105.  
  106. //foreach
  107. //doesn't return anything
  108. // const arr = ['samim', 'Hasan']
  109. // let resultStr = ''
  110. //  arr.forEach((elm) => resultStr += elm)
  111.  
  112. // console.log(resultStr)
  113.  
  114. //slice doesn't modifies original array
  115. //last index is exclusive
  116.  
  117. // const animals = ['ant', 'bison', 'camel', 'duck', 'elephant'];
  118.  
  119. // console.log(animals.slice(2, 4));
  120. // console.log(animals)
  121.  
  122. //splice  modifies original array
  123. //last index is inclusive
  124. // const animals = ['ant', 'bison', 'camel', 'duck', 'elephant']
  125.  
  126. // console.log(animals.splice(2, 4))
  127. // console.log(animals)
  128.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement