A quick revision of some important JavaScript array functions using practical examples

Reduce, Filter, Includes, Map, ForEach

XQ
The Research Nest
5 min readMar 29, 2022

--

Photo by Blake Connally on Unsplash

One of the fundamental things we do in frontend development is to fetch the data from an API, do some operations or check for something in it and then display it on the UI.

This data is generally read as a JSON or an array of JSON. Some of the most common operations we try to perform on this data are as follows:

  • Extracting values meeting certain conditions using the Filter function.
  • Computing a value across an array using the Reduce function.
  • Modifying, creating, or comparing objects/values by iterating over the array using Map or ForEach.
  • Perform some operations with data to get some required results that are to be displayed on the UI.

Let’s take an example to quickly understand how to properly use these Javascript functions. Let’s say we are trying to build some UI dashboard showing cryptocurrency data that we get from Coinbase’s public APIs. Below is the kind of real data you might actually deal with in many different use cases.

Here’s another API that gives the exchange rate for the corresponding currency code with Bitcoin.

What kind of things we might need to do with this data?

Using Filter and Includes

Filter can be used whenever you want to check if some condition is satisfied for each item in the array and if yes, put them in a new array.

In our example, we want to get the currency code objects of a list of predetermined currencies from the entire list and you want to use those currency codes alone to render something.

Now, we would want to filter the currency codes from the big JSON list (first API) where the currency belongs to our required list.

The arrow notation is a convenient way to write inline functions like the one above. Also, notice the use of array.includes() function. It will return true if the given array contains the value we pass to it.

In our case, if the currency name is in the required currencies, the filter function will add that currency object to the filteredData.

Alternatively, if you want to use a return statement in the arrow notation, you can use curly braces.

This approach is applicable to any situation where you plan to return a value while using arrow notation.

Using Map and ForEach

Now, we want to create an array of label-value pairs from the data. Let the label be the currency name and the value be the current BTC exchange rate for that currency.

We may need such label-value pairs to pass it on to a dropdown select box or some radio/checkbox options.

Let’s assume currencies is the JSON array of all currency codes and rates is the JSON array of the exchange rates for the corresponding currency codes (check the second API). We can get our label value pairs as follows.

So, array.map executes a function and returns a value for each item of the array. These values are stored in a new array. Here’s what the mapped LabeledData looks like (Showing only the first few items).

This can now be conveniently passed on to a component like a select box or used in some UI rendering. ForEach() can also be used in the exact same way, the difference being, it modifies the original array with returned values and no new array is created.

Using Reduce

Let’s say we have a portfolio JSON which consists of all the cryptocurrencies owned by a user. We want to calculate and show the net worth of their crypto portfolio in USD. Assume you have the USD exchange rate for each cryptocurrency within this JSON.

(Values are dummy values)

Simple observation tells us this.

The same operation can be performed on an array using the reduce() function as follows.

This way, you can compute a single value across the array. The function also takes an initial value to start with.

Summary

  • array.filter((item) => some condition); is used to extract items from an array meeting specific conditions.
  • array.includes(value) returns True if the value is present in the given array.
  • array.map((item) => some function); executes the given function on each item of the array and the corresponding results are stored in a new array.
  • array.forEach((item) => some function); does the same thing as array.map but it modifies the existing array and does not create a new one.
  • array.reduce((value, item) => some function/operation, initialValue); executes the given function for each item in the array and passes on the return value to the next iteration. This way, the return value can be accessed and updated at each iteration giving a final value at the end of the loop.

--

--

XQ
The Research Nest

Exploring tech, life, and careers through content.