Sample Video Frame

Created by Zed A. Shaw Updated 2024-02-17 04:54:36

26: Transforming Data

Functional Programming in JavaScript is limited mostly to using "data transforms" and callbacks. As you learned in Exercise 25 you can't really do recursion, but you can use functions to change data easily. What you can do reliably is transform data using four functions:

  • map -- Apply a calculation on each element.
  • reduce -- Turn a sequence into a final calculation.
  • find -- Search for an element in a sequence.
  • filter -- Filter out selected elements from a sequence.
  • forEach -- Just loop over a sequence without a result.

I'm using the word "sequence" since these functions should be on most anything that works like an Array ...maybe...if they did it right. Let's look at each one:


Calling .map on a sequence will produce a new sequence with a change to each value based on the result of a callback:

> let x = [1,2,3,4];
> => i + 10);
[ 11, 12, 13, 14 ]

You can see I passed a callback to .map that simply added 10 to the numbers in x and produces a new Array with [11, 12, 13, 14]. Remember that in simple callbacks like this you don't need return since the result will be returned by default.

Previous Lesson Next Lesson

Register for Learn JavaScript the Hard Way

Register today for the course and get the all currently available videos and lessons, plus all future modules for no extra charge.