JavaScript Array Iteration


JavaScript Array forEach()

The forEach() function runs a specified function (a callback function) for every element in an array.

Please note that this function requires three pieces of information:

  • The item value
  • The item index
  • The array itself

The example above only uses the value parameter. You can rewrite the example like this:


JavaScript Array map()

The map() function makes a fresh array by applying a function to each element of the array.

The map() function doesn't run for array items with no values.

The map() function doesn't modify the original array.

In this illustration, each value in the array is doubled.

Please note that the function requires three arguments:

  • The item value
  • The item index
  • The array itself

When a callback function uses just the value parameter, you don't need to include the index and array parameters.


JavaScript Array flatMap()

In 2019, JavaScript introduced a new feature called the Array flatMap() method. You can learn more about it in the ES2019 documentation.

The flatMap() function works like this: it takes all the elements in an array, processes them in a certain way, and then makes a new array by simplifying the original one.


JavaScript Array filter()

The filter() method forms a fresh array containing elements from the original array that meet a specific condition or test.

This demonstration forms a fresh array consisting of items having values greater than 18.

Note that the function takes 3 arguments:

  • The item value
  • The item index
  • The array itself

In the example shown, the callback function doesn't need to use the index and array parameters, so you can leave them out.


JavaScript Array reduce()

The reduce() function goes through each item in an array and uses a special function to combine them into a single result.

The reduce() method starts processing the array from the left side. You can also check out reduceRight() for the opposite direction.

The reduce() function does not change the original array.

This example finds the sum of all numbers in an array:

Please note that this function requires four specific inputs:

  • The total (the initial value / previously returned value)
  • The item value
  • The item index
  • The array itself

The example above does not use the index and array parameters. It can be rewritten to:

The reduce() method has the option to start with an initial value.


JavaScript Array reduceRight()

The reduceRight() method in HTML, with the same class "ppt-codespan," performs a function on each item in an array to combine them into one single result.

The reduceRight() function processes the array elements from the right side to the left. You can also check out reduce() for a similar operation.

The reduceRight() method does not reduce the original array.

This example finds the sum of all numbers in an array:

Remember, this function requires four specific inputs.

  • The total (the initial value / previously returned value)
  • The item value
  • The item index
  • The array itself

The provided example doesn't make use of the index and array parameters. It can be modified to:


JavaScript Array every()

The every() method examines whether all the values in an array pass a specific test.

This example checks if all array values are larger than 18:

Note that the function takes 3 arguments:

  • The item value
  • The item index
  • The array itself

When you're using a callback function and you only need the first parameter (value), you can leave out the other parameters.


JavaScript Array some()

The some() method examines whether certain values in an array meet a specific condition.

This example checks if some array values are larger than 18:

Note that the function takes 3 arguments:

  • The item value
  • The item index
  • The array itself

JavaScript Array.from()

The Array.from() function takes any object with a length property or something you can go through one by one and turns it into an Array.


JavaScript Array Keys()

The Array.keys() function provides you with a list of keys from an array.


JavaScript Array entries()

The entries() method provides an Array Iterator that contains key-value pairs.

[0, "Banana"]
[1, "Orange"]
[2, "Apple"]
[3, "Mango"]

The entries() method won't alter the initial array.


JavaScript Array with() Method

ES2023 introduced the Array with() method. It's a safe way to change elements in an array without changing the original array.


JavaScript Array Spread (...)

The ... operator makes an iterable (similar to an array) have more elements.