Arrays + functions
Combine arrays, a very useful data structure, with the power of functions, to do awesome things.
Weāve seen arrays, and functions.
Now imagine combining the two.
Thatās what happens with some very useful built-in methods you can use on any array:
map()
filter()
ā¦ and many others
I think they are super essential you know them inside out in JavaScript.
map()
Map is used to loop over an existing array and returns a new array, with the result of running a function for each item of the array.
Suppose we have an array named a
:
We create another array named b
, which is completely new, calling the map()
method of the array a
, using this syntax:
If you check what b
contains, youāll see it has 3 items in it, all with the value undefined
.
Why undefined
? Because we didnāt return anything from the function we passed to map()
.
If you returned the number 5
from that function, youād have in b
an array with 3 times the number 5:
Things start to be useful when you realize the callback function (the function we pass to map()
) receives each itemās value, and you can do many things now.
To start with, you can return the item, so you basically clone the original array, provided itās filled with primitive values:
Now b
is a copy of a
.
You can choose to have in b
the result of multiplying each item in a
by 10:
You donāt just get the item in the function, you also get its index, and the original array too:
filter()
With filter()
you can create a new array from an existing one, but contrary to map()
you can choose which items you want to include.
How do you choose? In the callback function that filter()
accepts, you return true
to include that item in the new array.
Suppose you have an array with some numbers:
and you want to create an even
array with just the even numbers.
Hereās how to do that:
We can write it in a shorter form:
This method return an array, so you can assign it to a new variable:
even
is an array containing [2, 4]
.
A very good example of filter()
in practice is to find a specific element in the array:
I used shift() because filter() returns an array, and shift() returns the first item in the array, or undefined if the array is empty.
Another practical example of using filter() is when you want to remove an item from the array. Suppose you want to remove the number 3
from the array:
You can also change this slightly to remove multiple items, using includes()
:
reduce()
Calling the reduce()
method on an array allows us to transform that array to anything else.
Like a number, or a boolean.
The reduce()
function wants 2 parameters: a function, and a value, which we call accumulator.
Hereās an example.
reduce()
does 4 iterations on the array.
Notice the accumulator
value received by the function. Its initial value is the second parameter of reduce()
, which is in this case 1
.
In the first iteration, accumulator
is 1
and it returns 1 * 1 = 1
.
In the second iteration, accumulator
is 1
and it returns 1 * 2 = 2
.
In the third iteration, accumulator
is 2
and it returns 2 * 3 = 6
.
In the fourth iteration, accumulator
is 6
and it returns 6 * 4 = 24
.
The value or result
is 24.
Letās do another example. We sum the numbers in an array:
Notice I didnāt pass the initial value of the accumulator. When this happens, reduce() uses the default, which is
0
The value of result
is 10
.
Hereās an example that works with an array of objects.
We sum the values of all the content.value
property contained in this array:
The code can written as
Which is equivalent to using this loop:
sort()
You can use sort()
to sort an array alphabetically:
This does not work for numbers, as it sorts for ASCII value (0-9A-Za-z
)
You can sort by number value using a custom function:
Reverse the sort order of an array using reverse()
:
find() and findIndex()
find()
is used to find an item in the array.
We pass a function to if, and we get back the first item that returns true
from it.
Returns undefined if not found.
Example:
findIndex
is similar but instead of the item, like find()
, it returns the index of the first item that returns true, and if not found, it returns undefined
:
forEach()
The forEach()
of an array instance is used to execute a function, which we call ācallback functionā, for all elements of an array. This callback function is passed the current item:
Itās similar to map()
, however nothing is returned from forEach
while using map
you get a new array with the result of the function executed in the callback.
You can also get the index of the element as the second parameter to the callback function:
Last updated
Was this helpful?