JavaScript Array Iteration
- Previous Page JS Array Sorting
- Next Page JS Array Const
Array iteration methods operate on each array item.
Array.forEach()
forEach()
The method calls the function once for each array element (callback function).
Example
var txt = ""; var numbers = [45, 4, 9, 16, 25]; numbers.forEach(myFunction); function myFunction(value, index, array) { txt = txt + value + "<br>"; }
Note:The function accepts 3 parameters:
- Item value
- Item index
- The array itself
The above example only used the value parameter. This example can be rewritten as:
Example
var txt = ""; var numbers = [45, 4, 9, 16, 25]; numbers.forEach(myFunction); function myFunction(value) { txt = txt + value + "<br>"; }
All browsers support it Array.forEach()
except for Internet Explorer 8 or earlier versions:
Yes | 9.0 | Yes | Yes | Yes |
Array.map()
map()
The method creates a new array by executing a function on each array element.
map()
The method does not execute the function on array elements without values.
map()
The method does not change the original array.
This example multiplies each array value by 2:
Example
var numbers1 = [45, 4, 9, 16, 25]; var numbers2 = numbers1.map(myFunction); function myFunction(value, index, array) { return value * 2; }
Please note that the function has 3 parameters:
- Item value
- Item index
- The array itself
When the callback function only uses the value parameter, the index and array parameters can be omitted:
Example
var numbers1 = [45, 4, 9, 16, 25]; var numbers2 = numbers1.map(myFunction); function myFunction(value) { return value * 2; }
All browsers support it Array.map()
except for Internet Explorer 8 or earlier versions:
Yes | 9.0 | Yes | Yes | Yes |
Array.filter()
filter()
The method creates a new array containing array elements that pass the test.
This example creates a new array with elements greater than 18:
Example
var numbers = [45, 4, 9, 16, 25]; var over18 = numbers.filter(myFunction); function myFunction(value, index, array) { return value > 18; }
Please note that this function accepts 3 parameters:
- Item value
- Item index
- The array itself
In the above example, the callback function does not use the index and array parameters, so they can be omitted:
Example
var numbers = [45, 4, 9, 16, 25]; var over18 = numbers.filter(myFunction); function myFunction(value) { return value > 18; }
All browsers support it Array.filter()
except for Internet Explorer 8 or earlier versions:
Yes | 9.0 | Yes | Yes | Yes |
Array.reduce()
reduce()
The method runs a function on each array element to generate (reduce it) to a single value.
reduce()
The method works from left to right in the array. See also reduceRight().
reduce()
The method does not reduce the original array.
This example determines the sum of all numbers in the array:
Example
var numbers1 = [45, 4, 9, 16, 25]; var sum = numbers1.reduce(myFunction); function myFunction(total, value, index, array) { return total + value; }
Please note that this function accepts 4 parameters:
- Total (initial value / previously returned value)
- Item value
- Item index
- The array itself
The previous example did not use index and array parameters. It can be rewritten as:
Example
var numbers1 = [45, 4, 9, 16, 25]; var sum = numbers1.reduce(myFunction); function myFunction(total, value) { return total + value; }
reduce()
The method can accept an initial value:
Example
var numbers1 = [45, 4, 9, 16, 25]; var sum = numbers1.reduce(myFunction, 100); function myFunction(total, value) { return total + value; }
All browsers support it Array.reduce()
except for Internet Explorer 8 or earlier versions:
Yes | 9.0 | Yes | Yes | Yes |
Array.reduceRight()
reduceRight()
The method runs a function on each array element to generate (reduce it) to a single value.
reduceRight()
The method works from right to left in the array. See also reduce().
reduceRight()
The method does not reduce the original array.
This example determines the sum of all numbers in the array:
Example
var numbers1 = [45, 4, 9, 16, 25]; var sum = numbers1.reduceRight(myFunction); function myFunction(total, value, index, array) { return total + value; }
Please note that this function accepts 4 parameters:
- Total (initial value / previously returned value)
- Item value
- Item index
- The array itself
The previous example did not use index and array parameters. It can be rewritten as:
Example
var numbers1 = [45, 4, 9, 16, 25]; var sum = numbers1.reduceRight(myFunction); function myFunction(total, value) { return total + value; }
All browsers support it Array.reduceRight()
except for Internet Explorer 8 or earlier versions:
Yes | 9.0 | Yes | Yes | Yes |
Array.every()
every()
The method checks if all array values pass the test.
This example checks if all array values are greater than 18:
Example
var numbers = [45, 4, 9, 16, 25]; var allOver18 = numbers.every(myFunction); function myFunction(value, index, array) { return value > 18; }
Please note that this function accepts 3 parameters:
- Item value
- Item index
- The array itself
If the callback function only uses the first parameter (value), the other parameters can be omitted:
Example
var numbers = [45, 4, 9, 16, 25]; var allOver18 = numbers.every(myFunction); function myFunction(value) { return value > 18; }
All browsers support it Array.every()
except for Internet Explorer 8 or earlier versions:
Yes | 9.0 | Yes | Yes | Yes |
Array.some()
some()
The method checks if some array values pass the test.
This example checks if some array values are greater than 18:
Example
var numbers = [45, 4, 9, 16, 25]; var someOver18 = numbers.some(myFunction); function myFunction(value, index, array) { return value > 18; }
Please note that this function accepts 3 parameters:
- Item value
- Item index
- The array itself
All browsers support it Array.some()
except for Internet Explorer 8 or earlier versions:
Yes | 9.0 | Yes | Yes | Yes |
Array.indexOf()
indexOf()
The method searches for an element value in an array and returns its position.
Note:The position of the first item is 0, the position of the second item is 1, and so on.
Example
Search for the item "Apple" in the array:
var fruits = ["Apple", "Orange", "Apple", "Mango"]; var a = fruits.indexOf("Apple");
All browsers support it Array.indexOf()
except for Internet Explorer 8 or earlier versions:
Yes | 9.0 | Yes | Yes | Yes |
Syntax
array.indexOf(item, start)
item | It is essential. The items to be searched. |
start | Optional. Where to start the search. Negative values start from the given position from the end and search to the end. |
If the item is not found,Array.indexOf()
Returns -1.
Returns the position of the first occurrence of the item if it appears multiple times.
Array.lastIndexOf()
Array.lastIndexOf()
with Array.indexOf()
Similarly, but it starts searching from the end of the array.
Example
Search for the item "Apple" in the array:
var fruits = ["Apple", "Orange", "Apple", "Mango"]; var a = fruits.lastIndexOf("Apple");
All browsers support it Array.lastIndexOf()
except for Internet Explorer 8 or earlier versions:
Yes | 9.0 | Yes | Yes | Yes |
Syntax
array.lastIndexOf(item, start)
item | It is essential. The items to be searched. |
start | Optional. Where to start the search. Negative values start from the given position at the end and search to the beginning. |
Array.find()
find()
The method returns the value of the first array element that passes the test function.
This example finds (returns) the value of the first element greater than 18:
Example
var numbers = [4, 9, 16, 25, 29]; var first = numbers.find(myFunction); function myFunction(value, index, array) { return value > 18; }
Please note that this function accepts 3 parameters:
- Item value
- Item index
- The array itself
Older browsers do not support Array.find()
The following lists the first browser version that fully supports this method:
45 | 12 | 25 | 8 | 32 |
Array.findIndex()
findIndex()
The method returns the index of the first array element that passes the test function.
This example finds the index of the first element greater than 18:
Example
var numbers = [4, 9, 16, 25, 29]; var first = numbers.findIndex(myFunction); function myFunction(value, index, array) { return value > 18; }
Please note that this function accepts 3 parameters:
- Item value
- Item index
- The array itself
Older browsers do not support Array.findIndex()
The following lists the first browser version that fully supports this method:
45 | 12 | 25 | 8 | 32 |
- Previous Page JS Array Sorting
- Next Page JS Array Const