Arrays

Array is a convenient data structure that helps us group objects together. For instance, an Array could be holding on to names of countries.

['Norway', 'Sweden', 'Denmark']

More often than not you will probably want to do something with the elements in Arrays. In the case above we could imagine that we wanted to find all the countries that start with the capital letter N, or transform all countries to upper-case. JavaScript provides us with a powerful tool to accomplish these feats, namely methods.

This section will cover some of the methods we can call on an Array.

Filter

The filter method enables us to retain elements that fulfill some condition.

Let's say we wanted to create some sweet lemon juice. To ensure that pulp and seed do not end up in the pitcher we need to filter it out.

By appending a .filter to the Array we can specify the criterion that must be fulfilled for the element to be retained. Just like we use a strainer to when squeezing a lemon, we use `.filter` on the elements in an Array.

['juice', 'seed', 'pulp'].filter(ingredient => ingredient === 'juice');

The resulting Array would only contain juice.

Map

Another useful method is map. It allows us to transform the elements of an Array.

Consider an Array of countries. If we wanted to make all of them upper-case we could use `.map`.

['Norway', 'Sweden', 'Denmark'].map(country => country.toUpperCase());

This would result in the following Array.

['NORWAY', 'SWEDEN', 'DENMARK']

Some

Whenever we have a collection of something it can often be useful to determine if it contains a single element that fulfills some condition. This method differs from the previous ones in its return value.

Imagine an Array of integers. To determine if the Array contains any even numbers we could use .some.

[1, 3, 5, 7].some(integer => integer % 2 === 0)

This would return false, as we do not have any integer that is even. Conversely, the following Array would return true.

[1, 3, 5, 4].some(integer => integer % 2 === 0)

Every

It can be just as useful to determine if all the elements of an Array fulfill some criterion.

Building on the previous example of integers, imagine we wanted to determine if all the integers of an Array are even.

[2, 4, 6, 8].every(integer => integer % 2 === 0)

This would return true, as all the integers are even. Conversely, the following Array would return false.

[2, 4, 6, 7].every(integer => integer % 2 === 0)

forEach

Sometimes you may not want not want to retain, transform or determine anything on an Array — but rather call something different for each element.

Let's say we wanted to console.log each country name. Although we could accomplish this using .map or .filter, it would not make a lot of sense considering that they are intended for transforming and retaining elements. Instead we use .forEach.

['Norway', 'Sweden', 'Denmark'].forEach(country => console.log('Country: ' + country));

This code would not return an Array nor a boolean, instead it would log the following.

Country: Norway
Country: Sweden
Country: Denmark

Last updated