Iterators

Iterators are built-in Array functions. They iterate through an array and use a callback to do something to, or with, the the values in that array.

Couldn't we just use for-loops? Absolutely! But writing for loops is error prone and tiring, which is why Javascript provides these iterators to perform common operations for us.

  • run some piece of logic on each item

  • create a new array with each item being slightly transformed

  • filter the array to contain only a subset of items

  • combine all the items in some fashion

The iterators for these operations are, respectively:

General Practice

  1. Declare an array

  2. Call an iterator on the array

  3. Pass a function to the iterator

  4. Get results

forEach

What

forEach is the basic replacement for your standard for loop.

How

Take the body from your for loop, wrap it in a function, and pass that as the callback argument to forEach. This iterator will take each array value and one-by-one pass these values into your callback function.

Examples:

Try it

Use the .forEach iterator to loop over the following array of foods and say you like them.

Try it again

Use the .forEach iterator to loop over the following array of objects and say how delicious each one is.

map

What:

Use the values from an array to build a new array. In other words: Map the values from one array into another new array and return that new array.

How:

Like forEach, map will one-by-one pass the values from the array into your callback function. You must return a value in your callback function, and this will be the value that appears in the new array.

Examples:

Create a new array where all the values from the old array are capitalized.

Use map to create an array of objects with a firstName property and a lastName property

Challenge: Modify splitName to account for the possibility of a middle name that will store as a middleName property.

Use map to create a new array strNums that holds the same values as intNums but as strings instead of integers

more on map

filter

What:

Returns a subset of the original array by iterating through the original array and filtering out values.

How:

Your callback must return a boolean. filter will one-by-one pass the values from the array into your callback. If the callback returns true, that element is included in the returned new array, otherwise it is excluded.

Examples:

Use filter to get 2 new arrays - one that contains names of even length only and one that contains names of odd length only

Use filter to return an array of dogs.

reduce

What:

Iterates over an array and turns it into one, accumulated value. In other words, you reduce a collection of values into one value. (In some other languages it is called fold.)

How:

Your callback must take two arguments: (1) accumulated value/total (2) new/original array value. The value that your callback returns will be the new total.

By default, total will start out as the 0th element in the array and new will be the element at index 1.

Example

Alternative Initial Value

If you want to start with a different total than 0th element, you can pass a second argument into filter, and it will start by passing this value in as total, and the 0th element as new.

Resources

Here are some good blog posts that break down map, filter, and reduce.

Last updated

Was this helpful?