• Social

Write your own JavaScript reduce function

The JS reduce function is a powerful utility in any developers tool belt and as such it’s crucial to have a good understanding of it. To get an understanding, there is no better way than to build your own.

Imagine we want to get the sum of an array of numbers. Using the native reduce function we would write the following.

[pastacode lang=”javascript” manual=”%2F%2F%20native%20reduce%20function%0A%0Aconst%20numbers%20%3D%20%5B1%2C2%2C3%5D%0A%0Aconst%20sumReducer%20%3D%20(initialValue%2C%20current)%20%3D%3E%20initialValue%20%2B%20current%3B%0A%0Aconst%20sumOfNumbersNative%20%3D%20numbers.reduce(sumReducer%2C%200)%0A%0Aconsole.log(‘Native%20reduce%20function%2C%20’%20%2B%20sumOfNumbersNative)%0A%0A%2F%2F%20Native%20reduce%20function%2C%206″ message=”” highlight=”” provider=”manual”/]

As you can see, the reduce function takes two arguments, firstly a callback, and secondly the initial value (a number, or an object for example). The callback is your ‘reducer’, which takes 4 arguments, the accumulator, currentValue, index and array. As you can see in the above example our sumReducer only needs the accumulator and the current value as it is only a simple version.

The initial 0 value is passed into our reduce function, which is then passed into our sumReducer function and added up with all the other numbers to return the number 6.

Be careful to note the difference between the main reduce function and the reducer that you pass into it. The reducer is the logic that you run your reduce with.

Let’s take a look at how we would build a custom reduce function to hopefully clarify things a bit. You should notice that we are looping over the array we pass into it, and running those values through a reducer that will be defined when we use the new reduce function.

[pastacode lang=”javascript” manual=”%2F%2F%20custom%20reduce%20function%0A%0Aconst%20numbers%20%3D%20%5B1%2C2%2C3%5D%0A%0Aconst%20sumReducer%20%3D%20(initialValue%2C%20current)%20%3D%3E%20initialValue%20%2B%20current%3B%0A%0Aconst%20reduce%20%3D%20(reducer%2C%20initialValue%2C%20array)%20%3D%3E%20%7B%0A%20%20let%20value%20%3D%20initialValue%3B%0A%20%20%0A%20%20for(let%20i%20%3D%200%3B%20i%20%3C%20array.length%3B%20i%2B%2B)%20%7B%0A%20%20%20%20let%20currentValue%20%3D%20array%5Bi%5D%0A%20%20%20%20value%20%3D%20reducer(value%2C%20currentValue)%0A%20%20%7D%0A%20%20%0A%20%20return%20value%3B%0A%7D%0A%0Aconst%20sumOfNumbersCustom%20%3D%20reduce(sumReducer%2C%200%2C%20numbers)%0A%0Aconsole.log(%22Custom%20reduce%20function%2C%20%22%20%2B%20sumOfNumbersCustom)%3B” message=”” highlight=”” provider=”manual”/]

You can see from the above that all your are doing is running an array of values through your desired logic to get a single output. This single output, could be a number, an array or an object. Similar to how you reduce your tomato sauce down by boiling it, we are reducing our array into a single desired thing. This is a powerful tool that can be utilised on a much more complex items, for example creating a nicely organised JSON object out of a string of data. If you don’t understand it the first time round, keep practicing and eventually it will stick and become second nature.