Fireship · @fireship

Array Reduce in 100 seconds

Uploaded December 26, 2019 · Length 1:41

Array Reduce in 100 seconds

Summary

learn how to use array reduce to compute a single value from a list of items in javascript

  • array reduce
  • javascript reduce
  • array methods
  • javascript array
  • programming basics

Transcript preview

The first 2 minutes of the transcript. Select a timestamp to watch that moment on YouTube.

  1. 0:00

    arrey reduce start with a list of items

  2. 0:01

    then iterate over them to compute a

  3. 0:04

    single value it works just like basic

  4. 0:06

    arithmetic consider one plus two plus

  5. 0:08

    three what we have here is a list of

  6. 0:10

    numbers they're reduced to a single

  7. 0:12

    value using a function called addition

  8. 0:14

    in arithmetic we go from left to right

  9. 0:16

    the results of the first computation is

  10. 0:18

    then added to the next one so it's

  11. 0:20

    actually just a loop but a loop with a

  12. 0:22

    memory take for example in array of

  13. 0:25

    orders from our online store our goal is

  14. 0:27

    to calculate the total dollar amount of

  15. 0:28

    sales across all of the orders in this

  16. 0:30

    array one option is to create a variable

  17. 0:33

    that starts at zero then we'll start a

  18. 0:34

    for loop and increment this value for

  19. 0:36

    each order total reduce can solve the

  20. 0:38

    same type of problem but without

  21. 0:40

    mutating the appstate directly it does

  22. 0:42

    this by applying a reducer function to

  23. 0:45

    each element in that array your job is

  24. 0:47

    to define the reducer function the

  25. 0:49

    signature of that function has two

  26. 0:50

    required positional arguments the first

  27. 0:52

    one being the accumulator that's just a

  28. 0:54

    fancy word for the last value that you

  29. 0:56

    returned from this function the second

  30. 0:58

    argument represents the current element

  31. 1:00

    in the loop or in our case and ordered

  32. 1:02

    object the way we change the accumulated

  33. 1:04

    or remembered value is to return a value

  34. 1:06

    from the function to combine all of the

  35. 1:08

    order totals into a single number we

  36. 1:10

    simply add the accumulated value to the

  37. 1:12

    current value and optionally we can add

  38. 1:15

    a starting value as a second argument

  39. 1:16

    which will set at zero the end result is

  40. 1:19

    the combined order total across all

  41. 1:20

    elements in the array we started with

  42. 1:22

    many things and reduced it down to one

  43. 1:24

    thing in some cases you might want to

  44. 1:26

    start at the end of the array and work

  45. 1:27

    your way backwards you can use reduced

  46. 1:29

    write to start at the end of the array

  47. 1:30

    and work your way to the beginning this

  48. 1:33

    has been array reduce and 100 seconds

  49. 1:35

    thanks for watching and I will see you

  50. 1:37

    in the next one

  51. 1:38

    [Music]