If an initialValue was provided in the call to reduceRight, then accumulator will be equal to initialValue and currentValue will be equal to the last value in the array. We've got our reducer, and that's going to take the accumulator. Note: If initialValue isn't provided, reduce will execute the callback function starting at index 1, skipping the first index. We've got our accumulator, and that accumulator's going to be an object. If no initialValue was provided, then accumulator will be equal to the last value in the array and currentValue will be equal to the second-to-last value. Then vote is just going to be each of these items in a row. Vote is Angular again, and tally is the return value from the previous iteration.
We're going to say if the tally does not have a key with the same name as that vote, then we're going to create one. It's going to take our reducer, and it's going to take our initial value. Specifications Specification Status Comment Standard Initial definition. Now when we get to our logic we're going to see it does exist, so we're going to increment that. Let's set our initial value for that accumulator and it's just an empty object.
If initialValue is provided in the call to reduce , then accumulator will be equal to initialValue, and currentValue will be equal to the first value in the array. Tally is this list of Angular is two. The source for this interactive example is stored in a GitHub repository. Here's how we're going to do this. It is usually safer to provide an initial value because there are three possible outputs without initialValue, as shown in the following example.
It's got two keys, Angular whose value is two, and React, whose value is one and so on and so forth. Starts at index 0, if an initialValue is provided, and at index 1 otherwise. Calling reduce on an empty array without an initial value is an error. The vote here is going to be Angular, and the tally is going to be this empty object. You can also provide an in lieu of a full function. Calling reduce on an empty array without an initial value is an error. The votes start pouring in.
Now we're returning an object whose key is Angular and whose value is two. It's an object with one key, Angular, whose value is one. Hey, this is a job for Reduce. If no initialValue is provided, then accumulator will be equal to the first value in the array, and currentValue will be equal to the second. If you'd like to contribute to the interactive examples project, please clone and send us a pull request.
Because this is reduced this whole thing evaluates to the value that gets returned from the very last call of the Reduce function. If the array is empty and no initialValue was provided, would be thrown. Let's call it the tally, and it's going to get each of these votes one after the other. If the array is empty and no initialValue is provided, will be thrown. Return value The value that results from the reduction. If the array has only one element regardless of position and no initialValue is provided, or if initialValue is provided but the array is empty, the solo value will be returned without calling callback. When we run this, we see we had three votes for Angular, four votes for React, one vote for Amber, and one vote for no framework.
If no initial value is supplied, the first element in the array will be used. The call to the reduceRight callback would look something like this: array. } ; The first time the function is called, the accumulator and currentValue can be one of two values. We're going to reduce it using this reducer. You got some votes for Angular. This reducer's going to take the accumulator and it's going to take the first item in the array. All right, so let's say you just went to your local front-end programming meetup and you took a vote.
We're going to set its value equal to one. Remember the way this works is tally here is whatever got returned the last time this reducer was run, except on the very first item where the tally is the initial value. We're going to step through every item here, and we're going to integrate each of those items one at a time into our running tally. If no initial value is supplied, the last element in the array will be used. Return value The value that results from the reduction. However, if the tally already has a value for that particular vote, we're just going to increment it.