6.15 Reducing an Array

Again with our number table, let's say that we want to calculate the sum of all the numbers in this numbers in this array. This is like calculating the total cost of all the items in a basket. So each number here should represent the price of an item in a basket, of course we wouldn't have a negative would not have a negative number, but you get the point. So here is a very simple algorithm to calculate the sum of all the numbers in this the numbers in this array. You start by declaring a variable called sum and we initialize it to 0. Then we go through the array, get each element for each number and add it to the sum. add it to the sum. Something like this. for let n of numbers, we now want to add n to the sum. So we can write an expression like this. Sum = sum + n, or a better way is to use the assignment operator. So we can exclude the second sum, so it's exactly like the statement we had before, and finally we we had before, and finally we display the sum on the console. So we get 5. Now there is a cleaner and more elegant way to write the and more elegant way to write the same code using the reduce method in arrays. So all these arrays have this reduce method, and with this method, we can reduce all these elements of an array into a single value. This single value can be a number, it can be a string, it can be an object string, it can be an object, it can be anything. In this example, we want to want to reduce all these elements to a single number, that is the sum of all the numbers in this array. So let's see how to use the reduce method. This method takes a call back function with two parameters. Accumulator and currentvalue. Here I use an arrow function like this. This accumulator parameter here is exactly like this sum that we have here. It's something that we initialize and this call back function is executed multiple times, every time this current value is set to an element of this array. So each time we call it, we want to get that current value and add it to the accumulator. So we return simply the sum of the accumulator plus the current value. Now, internally, this method method will get this result and store it in the accumulator, as you'll see in you'll see in a moment. One last thing here is to initialize this accumulator to 0. So, as the second argument to the reduce method, we pass 0, we pass 0. Note that this reduce method has two arguments, the first argument is a call back function and the second argument is the initial value of the accumulator. Finally, we get the result as a single value. In this case, sum. Now we don't need this now we don't need this code anymore and, finally, let's bring this console.log here. Save the changes, you can see that we get the same result 5, but let's see what exactly is going on here. So initially, we set the accumulator to 0, so I'm will set a = 0. In the first round, the current value will be set to the first number. So, c = 1. Now, we add that together, so we get 1 and a will be set to the first number. together, so we get 1 and a will be set to 1 after we run this of this call back function. So, therefore, a will be equal to 1. Now, in the second round, a is equal to 1, the current value will be set to the second number in this table. So, minus 1. We add them again, and a will be equal to 0 after the second call. Now the third call, so a equals 0, the current value will be set to the third element of this array. So 2 and therefore a will be equal to 2, finally in the last call, we start with a set to 2, and the current value will be equal to 3. So the result will be equal to 5, and that's why we saw 5 on the console. So, basically with this reduced method, we start with an accumulator, and then we go through that array and convert all of those elements into a single value, which in this case is an this case an accumulator, or the sum of all the numbers in that array. This is how our our reduction algorithm works. We simply get the current value and add it to our accumulator. This is much cleaner and more cleaner and more elegant than declaring a sum, then looping over that array as let n of numbers and then adding n to the sum, which is a very old way of writing code. That's it for this video on reducing an array, see you in the next video Translated with www.DeepL.com/Translator (free version)