Higher Order Functions: Array Methods

Follow along with code examples here!

Table of Contents

Imperative vs. Declarative

Imperative code provides explicit instructions for exactly HOW to complete a task.

  • High control (you write every single line)

  • High effort (you write every single line)

Declarative code provides the desired solution without specifying HOW to get there.

  • Low control

  • Low effort

Array Iterators: forEach, filter, and map

The most commonly used and perhaps most powerful higher-order functions are these "iterator" array methods:

  • arr.forEach — useful for performing some repetitive task for each value in the source array

  • arr.filter — useful for testing every value in the source array and keeping only the values that pass the test (e.g. only the even numbers in a numbers array)

  • arr.map — useful for transforming every value in the source array into another value (e.g. capitalize every string in an array of sentences);

Let's take a look at each of them. We can see how they are used and how we can create our own versions of them.

forEach

arr.forEach(callback) iterates over the source array and invokes the given callback with each value, that value's index, and the source array arr as inputs.

Use forEach to execute callbacks that produce side-effects (like printing to the console or mutating values).

Examples:

In this example, we want to print a message about each value and its location in the fruits array:

In this example, we provide an anonymous inline function. We have chosen to ignore the index and source array inputs and just focus on each user value in the users array:

Challenge:

Implement your own forEach function that takes an array and a callback and invokes the callback on every value in the array. The callback should be invoked with 3 inputs for every value:

  1. The current value

  2. The current value's index in the source array

  3. The source array itself

Nothing is returned.

Solution

filter

arr.filter(callback) iterates over the source array, invoking the given callback with each value, index, and the source array arr as inputs. If the callback returns true, the value is added to an array which is returned.

Use filter when you want a copy of the source array with unwanted values removed.

Examples:

In this example, we want to know how many scores in an array of numbers are greater than or equal to 75. Using arr.filter, we can get a copy of the array containing only those passing scores and then read its length.

In this example, we will have an array of user objects. We want to get a copy of the array that only contains admins. Filter lets us check each object and only those with isAdmin: true will pass the test and be returned in the new array.

Challenge:

Implement your own filter function that takes an array and a test callback and returns an array. The test callback should be invoked with 3 inputs for every value:

  1. The current value

  2. The current value's index in the source array

  3. The source array itself

The test callback should be expected to return true or false. If test returns true, the value it was called on should be added to the returned array.

Solution

map

arr.map(callback) iterates over the source array, invoking the given callback with each value, index, and the source array arr as inputs. The value returned by the callback is added to an array which is returned.

Use map when you want a copy of the source array with each value converted to a new value.

Examples:

In this example, we have an array inchesArr containing values each representing a number of inches. Using arr.map, we transform each inches value into a string in the format x inches => y feet z inches. Each of these new strings is added to the array returned by map and stored in feetAndInches:

In this example, we take a users array full of user objects and extract all of the usernames into an array:

Challenge:

Implement your own map function that takes an array and a modify callback and returns an array. The modify callback should be invoked with 3 inputs for every value:

  1. The current value

  2. The current value's index in the source array

  3. The source array itself

The modify callback should be expected to return a value that should be added to the returned array.

Solution

Array Higher Order Methods

  • The important ones to know are:

    • .forEach(callback)

    • .map(modify)

    • .filter(test)

    • .find(test)

    • .findIndex(test)

    • .reduce(accumulator, startingValue)

  • Invoke these directly ON the Array:

  • These are Higher Order Methods because they are functions stored within an Array "Object", not as a stand-alone function.

.forEach(callback)

Iterate through an array, but don't make a copy

Callback Should Accept
Callback Should Return
.forEach returns

The current value. Its index and the entire array are optional

nothing

undefined

.filter(testCallback)

Get ALL elements in an Array that pass a given test

Callback Should Accept
Callback Should Return
.filter returns

The current value. Its index and the entire array are optional

a boolean used to determine if the current element is kept

a new array of matches (or an empty one)

.map(modifyCallback)

Make a copy of an array with changes to each value

Callback Should Accept
Callback Should Return
.map returns

The current value. Its index and the entire array are optional

modified version of the current value

the new array with all the changes

.find(testCallback)

Get the first matching element in an Array, or null

Callback Should Accept
Callback Should Return
.find returns

The current value. Its index and the entire array are optional

a boolean used to determine if the match is found or not

the found value or null

Remember, the return value of the callback must return a boolean!

.findIndex(testCallback)

Get the index of the first matching element in an Array, or null

Callback Should Accept
Callback Should Return
.findIndex returns

The current value. Its index and the entire array are optional

a boolean used to determine if the match is found or not

the found index or -1

.reduce(accumulatorCallback, startingValue)

"reduce" array values into a single value.

Callback Should Accept
Callback Should Return
.reduce returns

An accumulator and the current value

the next value of the accumulator (the eventual return value)

the final accumulated value

Advanced Stuff

Last updated