/

Writing JavaScript loops using map, filter, reduce, and find

Writing JavaScript loops using map, filter, reduce, and find

Learn how to perform common operations in JavaScript using map(), filter(), reduce(), and find() instead of using loops.

Loops are commonly used in programming languages to perform operations on arrays. You can iterate over the elements of an array and perform calculations or operations on them.

In this blog post, we’ll explore alternative approaches to using loops by leveraging functional programming concepts. We’ll specifically look at four powerful array functions: map(), filter(), reduce(), and find(). These functions offer a more declarative approach to array operations, as opposed to the traditional imperative approach.

Let’s quickly introduce these functions:

  1. map(): Returns a new array with the same length as the original array.
  2. filter(): Returns a new array with fewer items than the original array, based on a condition.
  3. reduce(): Returns a single value or object by performing a specific operation on the array.
  4. find(): Returns the first item in an array that satisfies a given condition.

Note that map(), filter(), and reduce() were introduced in ES5 and are supported in all modern browsers. However, find() was introduced in ES6/ES2015.

Execute an operation on every element with map():

Instead of using a loop to iterate over an array and perform an operation on each element, you can use the map() function. This function takes a callback function as an argument and applies that function to each element of the array. The result is a new array with the processed values.

For example, consider the following loop:

1
2
3
4
5
6
7
8
9
10
const performSomething = (item) => {
//...
return item;
};

const items = ['a', 'b', 'c'];

items.forEach((item) => {
performSomething(item);
});

This loop can be rewritten using map() as follows:

1
2
const items = ['a', 'b', 'c'];
const newArray = items.map((item) => performSomething(item));

By using map(), we generate a new array without modifying the original one, which aligns with the concept of immutability.

Finding a single element in an array:

If you need to search for a specific item in an array and return it, you can use the find() function (introduced in ES6). This function takes a callback function as an argument and returns the first item in the array that satisfies the given condition.

Here’s an example of using a loop to find an item:

1
2
3
4
5
6
7
8
9
10
11
const items = [
{ name: 'a', content: {/* ... */} },
{ name: 'b', content: {/* ... */} },
{ name: 'c', content: {/* ... */} }
];

for (const item of items) {
if (item.name === 'b') {
return item;
}
}

Using find(), the same functionality can be achieved using a more concise syntax:

1
const b = items.find((item) => item.name === 'b');

You can also achieve the same result using filter() (introduced in ES5):

1
const b = items.filter((item) => item.name === 'b').shift();

The shift() function returns the first item in the array without raising an error if the array is empty (returns undefined in that case). Note that shift() mutates the array returned by filter(), not the original array. If you prefer to avoid mutation, you can check if the array is not empty and retrieve the first item using b[0].

For learning purposes, let’s also explore how to accomplish the same functionality using reduce():

1
2
3
4
5
6
7
8
9
10
11
12
const items = [
{ name: 'a', content: {/* ... */} },
{ name: 'b', content: {/* ... */} },
{ name: 'c', content: {/* ... */} }
];

const b = items.reduce((result, item) => {
if (item.name === 'b') {
result = item;
}
return result;
}, null);

Note that filter() and reduce() iterate over all the items in the array, while find() will return the result faster.

Iterating over an array to count a property of each item:

If you want to calculate a single value based on a property of each item in an array, you can use reduce(). For example, if you want to sum the values of the content.value property for each item in the array, you can use reduce().

Consider the following array:

1
2
3
4
5
const items = [
{ name: 'a', content: { value: 1 } },
{ name: 'b', content: { value: 2 } },
{ name: 'c', content: { value: 3 } }
];

Traditionally, you would use a loop to iterate over the items and calculate the sum:

1
2
3
4
let count = 0;
for (const item of items) {
count += item.content.value;
}

Using reduce(), you can achieve the same result in a more concise manner:

1
const count = items.reduce((result, { content: { value } }) => result + value, 0);

In this case, reduce() takes a callback function that accumulates the sum by adding the value property of each item to the result. The second argument to reduce() is the initial value of the result, which is 0 in our example.

By utilizing map(), filter(), reduce(), and find(), you can perform common operations on arrays in a more declarative and concise manner. These array functions can greatly improve the readability and maintainability of your code.

tags: [“JavaScript”, “loops”, “map”, “filter”, “reduce”, “find”, “functional programming”]