30 Days Of JavaScript

Day 10: Higher Order Functions Part 1

Lesson 3: map

This is one of the most used higher order functions and it lets us transform the data in an array from one form into another. For example if we square each number in an array:

The key bit here is where we passed (number) => number * number in to the .map() function.

This is short hand notation for:

1.map((number) => {
2    return number * number

You can call number what ever you like, that's just the argument name I have chosen here. If we wanted to change an array of people object then person would be a better name.

Also note that map, unlike sort, does not change the initial array, it returns a new array.

One of the most common uses of map() is transforming objects. Let's take a look at a more complex example.

You can see here we have taken our array of complicated person objects and created an array of just ages.


Go Pro?

If you upgraded to pro, sign in here

  • About
  • Blog
  • Privacy
Looking to email me? You can get me on my first name at allthecode.co