Array Reduce in 100 seconds

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
arrey reduce start with a list of items then iterate over them to compute a single value it works just like basic arithmetic consider one plus two plus three what we have here is a list of numbers they're reduced to a single value using a function called addition in arithmetic we go from left to right the results of the first computation is then added to the next one so it's actually just a loop but a loop with a memory take for example in array of orders from our online store our goal is to calculate the total dollar amount of sales across all of the orders in this array one option is to create a variable that starts at zero then we'll start a for loop and increment this value for each order total reduce can solve the same type of problem but without mutating the appstate directly it does this by applying a reducer function to each element in that array your job is to define the reducer function the signature of that function has two required positional arguments the first one being the accumulator that's just a fancy word for the last value that you returned from this function the second argument represents the current element in the loop or in our case and ordered object the way we change the accumulated or remembered value is to return a value from the function to combine all of the order totals into a single number we simply add the accumulated value to the current value and optionally we can add a starting value as a second argument which will set at zero the end result is the combined order total across all elements in the array we started with many things and reduced it down to one thing in some cases you might want to start at the end of the array and work your way backwards you can use reduced write to start at the end of the array and work your way to the beginning this has been array reduce and 100 seconds thanks for watching and I will see you in the next one [Music]
Info
Channel: Fireship
Views: 185,863
Rating: undefined out of 5
Keywords: webdev, lesson, tutorial, 100 seconds of code, array reduce, js reduce, reducer function, javascript array, javascript reduce, javascript tutorial
Id: tVCYa_bnITg
Channel Id: undefined
Length: 1min 40sec (100 seconds)
Published: Thu Dec 26 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.