How to Reduce Javascript Arrays with array.reduce()

How to Reduce Javascript Arrays with array.reduce()

Javascript reduce() is an inbuilt array method that executes a callback function known as a reducer on each element of the array and results in a single output value. The reduce() method executes the function for each value of the array (non-empty array) from left to right.

Oftentimes, we find ourselves needing to reduce an array down to one of the many Javascript primitive types like a number, string, boolean, object, another array (we’ll see some examples of this later), or even a custom type defined in our project. Javascript reduce() is an inbuilt array method that executes a callback function known as a *reducer *on each element of the array and results in a single output value. The reduce() method executes the function for each value of the array (non-empty array) from left to right.

Image for post

The reduce() method has the following syntax:

let arr = [];

arr.reduce(callback(acc, curVal, index, src), initVal);

Parameters:

  1. Accumulator (acc)
  2. Current Value (curVal)
  3. Current Index (index)
  4. Source Array (src)
  5. Initial Value (initVal)

Accumulator | required: This is the accumulated value previously returned from the function or the initial value if it was supplied.

Current Value | required: This is the value of the current element in the array.

Current Index | optional: This is the index of the current element in the array.

Source Array | optional: This is the array object reduce() was called upon.

Initial Value | optional: This is the initial value to be passed to the function. If no initial value is supplied, the first element in the array will be used as the initial accumulator value and the second element becomes the current value

_Note: _If the array only has one element and no initial value is provided, or an initial value is provided but the array is empty, the single will be returned without calling the callback. Calling reduce() on an empty array without an initial value will throw a [TypeError_](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypeError)._

javascript programming web-development developer

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Why Web Development is Important for your Business

With the rapid development in technology, the old ways to do business have changed completely. A lot more advanced and developed ways are ...

Important Reasons to Hire a Professional Web Development Company

    You name the business and I will tell you how web development can help you promote your business. If it is a startup or you seeking some...

What Garbage Collection in JavaScript Is and How It Works

JavaScript values are allocated when things are created (objects, Strings, etc.) and freed automatically when they are no longer used. This process is called Garbage collection.

What Javascript Spread Operator is, How It Works and How to Use It

JavaScript spread operator is one of the more popular features that were introduced in ES6. This tutorial will help you understand it. You will learn what spread operator is and how it works. You will also learn how to use it to copy and merge arrays and object literals, insert data and more.

Top 10 Web Development Trends in 2020 | Web Development Skills

Top 10 Web Development Trends in 2020 will provide you with a detailed list of the trends which are currently being noticed. Upskilling to these trends will help you in landing onto better jobs in 2020-2021.