Array Methods: map, filter, reduce
The transformations you will reach for in every list you render.
Rendering a list means turning an array of data into an array of elements. These three methods do that work, and each one hands you something new instead of changing the array you gave it.
map
map runs your function once per item and collects the results into a new
array.
titles is a new array of three strings. noReturn is three undefined
values, because that arrow has a body and no return. The last line shows
shows[0] is still the object it always was.
map vs forEach
forEach also runs your function once per item, but it throws the results
away and returns undefined.
The first line reads undefined. The second reads the two titles. Use
forEach to do something with each item, and map when you want a result.
filter
filter keeps the items your function says yes to. Your function has to
answer with a condition, not a value.
Three of the four shows are rated 8 or above. The last line keeps all four,
because show.rating is an object and every object is truthy. Compare the
number you mean to compare.
find
find, from Working with Arrays, takes the same kind of function
as filter. It hands back the first match on its own, not a list.
find gives one show, so drama.title works. filter gives an array of two,
so dramas.title is undefined — an array has no title. Use find for one
show and filter for a list.
reduce
reduce turns a whole array into one value. Your function receives the value
so far and the current item, and returns the next value so far. The second
argument to reduce is where that value starts.
The total is 24.3 and the average reads 8.1. The last line reads
[object Object]8.47.2: with no starting value, the first show became the
sum, and adding a number to an object gives a string. Always pass the
starting value. It also stops reduce throwing on an empty array.
Chaining
Each method returns a new array, so you can hang the next one off it. Filter first, then map: there is no point transforming items you are about to drop.
The first chain gives three titles. The second gives an empty array: after
map, each item is a string, and title.rating on a string is undefined.
Methods that mutate
sort is not like map. It rearranges the array you gave it.
titles comes out in a new order even though nothing was assigned to it.
toSorted does the same job and leaves more alone.
push, pop, splice, sort, and reverse all change the array in place.
map, filter, slice, and toSorted all hand back a new one. toSorted
is recent, so older projects copy first with [...titles].sort(), which
Destructuring, Spread, and Rest covers. Both
still compare items as text, which Working with Arrays shows.
Common mistakes
- Using
forEachwhen you want a result. It always returnsundefined; usemap. - Braces in an arrow callback.
(show) => { show.title }returns nothing, somapgives youundefinedfor every item. - Sorting an array you did not copy.
sortchanges it in place; usetoSortedor copy first.
Exercise
This should log the title of every streaming show rated above 8, one per
line. Right now it logs each whole show object instead.
The output should read:
Severance
Shogun
Filter first, then map. Remember rating is an object.