Advertisement
Mitfreex

reduce-Method

Dec 13th, 2022
782
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* REDUCE METHOD */
  2. //reduce(function (accumulator, currentValue, currentIndex, array) { /* … */ }, initialValue)
  3.  
  4.  
  5. /*
  6. const array1 = [1, 2, 3, 4];
  7.  
  8. // 0 + 1 + 2 + 3 + 4
  9. const initialValue = 0;
  10. const sumWithInitial = array1.reduce(
  11.   (accumulator, currentValue) => accumulator + currentValue,
  12.   initialValue
  13. );
  14.  
  15. console.log(sumWithInitial);
  16. // expected output: 10
  17. */
  18.  
  19.  
  20. /*
  21. Why is important to declare initValue?
  22.  
  23. !!!
  24. The first time that the callback is run there is no
  25.  "return value of the previous calculation".
  26.  If supplied, an initial value may be used in its place.
  27.   Otherwise the array element at index 0 is used as
  28.   the initial value and iteration starts
  29.   from the next element (index 1 instead of index 0).
  30. */
  31.  
  32. //example without init value
  33. // average calc in array with reduce
  34.  
  35.  
  36. let result = [1, 2, 3].reduce((acc, x, index, arr) => (
  37.     acc + x / arr.length
  38. ));
  39.  
  40. console.log(result); // --> 2,6,
  41. //corret avg value should be 1+2+3=6/3=2 or 1/3+2/3+3/3=2
  42. //but the first zero-indexed element is used as initValue
  43. // operation start applying from the 1-indexed element
  44. console.log(1 + 2 / 3 + 3 / 3); // --> 2,6
  45.  
  46.  
  47. // with init value 0, correct way:
  48. // 0+1/3+2/3+3/3=2
  49.  
  50. let resultOk = [1, 2, 3].reduce((acc, x, index, arr) => (
  51.     acc + x / arr.length
  52. ), 0);
  53. console.log(resultOk); //--> 2
  54. //-------------------------------
  55.  
  56.  
  57.  
  58. //01 Returning Array:
  59.  
  60. // filter even with reduce:
  61.  
  62. let numbers = [20, 10, 5, 6, 7];
  63.  
  64. console.log(numbers.reduce((acc, el) => {
  65.  
  66.     if (el % 2 == 0) {
  67.         acc.push(el);
  68.     }
  69.  
  70.     return acc;
  71. }, []))
  72. //---------------------------------------
  73.  
  74.  
  75.  
  76. //02 return objet:
  77. let n = 100;
  78. let resObj = ['keyA', 'keyZ', 'key0', 'code00', 345].reduce((acc, curEl, ind, arr) => {
  79.    
  80.     n++;
  81.     acc[curEl] = n;
  82.     return acc;
  83. }, {'000':'xxx'});
  84.  
  85. console.log(resObj); //--> {345: 105, 000: 'xxx', keyA: 101, keyZ: 102, key0: 103, code00: 104}
  86.  
  87. //----- - -- -------------------------
  88.  
  89. //03 return string, {length} is instead of Array.length
  90.  
  91. let resStr = ['how', 'to', 'use', 'reduce', 'method', '!'].reduce((acc, x, index, { length }) => {
  92.     let space = ' ';
  93.     if (index == length -1) {
  94.         space='';
  95.     }
  96.     acc += space+x;
  97.    
  98.     return acc;
  99. }, 'Teach');
  100.  
  101. console.log(resStr);
  102.  
  103.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement