Last updated: Mar 1, 2024
Reading timeยท3 min
reduce()
map()
To get the index of all occurrences of an element in an array:
forEach()
method to iterate over the original array.const arr = ['a', 'b', 'a', 'c', 'a']; const indexes = []; arr.forEach((element, index) => { if (element === 'a') { indexes.push(index); } }); console.log(indexes); // ๐๏ธ [ 0, 2, 4 ]
We declared a new variable and initialized it to an empty array.
The indexes
variable will store the indexes of all occurrences of the element
in the array.
The function we passed to the Array.forEach method gets called with each element in the array.
On each iteration, we check if the current element is equal to the specified value.
If the condition is met, we push the current index into the indexes
array.
0
and the last element has an index of arr.length - 1
.If you have to do this often, define a reusable function.
function allOccurrences(arr, value) { const indexes = []; arr.forEach((element, index) => { if (element === value) { indexes.push(index); } }); return indexes; } const arr = ['a', 'b', 'a', 'c', 'a']; console.log(allOccurrences(arr, 'a')); // ๐๏ธ [ 0, 2, 4 ] console.log(allOccurrences(arr, 'b')); // ๐๏ธ [ 1 ] console.log(allOccurrences(arr, 'c')); // ๐๏ธ [ 3 ]
The function takes an array and a value as parameters and returns all of the indexes of the value in the array.
Alternatively, you can use the Array.reduce()
method.
reduce()
This is a three-step process:
Array.reduce()
method to iterate over the array.accumulator
array.const arr = ['a', 'b', 'a', 'c', 'a']; const indexes = arr.reduce((accumulator, current, index) => { if (current === 'a') { accumulator.push(index); } return accumulator; }, []); console.log(indexes); // ๐๏ธ [ 0, 2, 4 ]
The function we passed to the Array.reduce() method gets called for each element in the array.
accumulator
variable to an array because that's what we passed as the second argument to the reduce()
method.The value we return from the callback function gets passed as the accumulator
on the next iteration.
On each iteration, we check if the current element is equal to a specific value.
If the condition is met, we push the current index into the accumulator
array.
Otherwise, we return the accumulator
array as is.
After the last iteration, the indexes
array stores the index of all
occurrences of the element in the array.
map()
You can also use the map()
and filter()
methods to find the index of all
occurrences of an element in an array.
const arr = ['a', 'b', 'a', 'c', 'a']; const value = 'a'; const indexes = arr .map((element, index) => (element === value ? index : -1)) .filter(element => element !== -1); console.log(indexes); // ๐๏ธ [ 0, 2, 4 ]
The function we passed to the Array.map() method gets called with each element in the array.
The map()
method returns a new array containing the values returned from the
callback function.
const arr = ['a', 'b', 'a', 'c', 'a']; const value = 'a'; const indexes = arr.map((element, index) => element === value ? index : -1, ); console.log(indexes); // ๐๏ธ [ 0, -1, 2, -1, 4 ]
For each element that doesn't meet the condition, we return -1
.
The last step is to use the Array.filter()
method to remove the -1
values
from the array.
const arr = ['a', 'b', 'a', 'c', 'a']; const value = 'a'; const indexes = arr .map((element, index) => (element === value ? index : -1)) .filter(element => element !== -1); console.log(indexes); // ๐๏ธ [ 0, 2, 4 ]
The function we passed to the Array.filter method gets called with each element in the array.
The filter()
method returns a new array that only contains the elements that
meet the condition.
You can learn more about the related topics by checking out the following tutorials: