Find indexes of all occurrences of an item in a JavaScript array
This post will discuss how to find the indexes of all occurrences of an item in an array in JavaScript.
Finding the index of all occurrences of an item in an array is a common task in JavaScript programming. There are several ways to do it, depending on our preference and the complexity of our code. Here are some of the functions that we can use, along with some examples:
1. Using indexOf() function
We can use the Array.indexOf() function to search for the item in the array, starting from a given index. This function returns the first index where the item is found, or -1 if not found. We can use a while loop to repeat this process until we reach the end of the array. The indexOf() function takes an optional second argument that specifies the starting index for the search. By updating this argument in each iteration, we can find all the occurrences of the item. Here’s an example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
function findAllIndexes(arr, val) { var indexes = [], i = -1; while ((i = arr.indexOf(val, i + 1)) != -1) { indexes.push(i); } return indexes; } var arr = ["A", "B", "C", "A", "D", "A"]; var indexes = findAllIndexes(arr, "A"); console.log(indexes); // [0, 3, 5] |
2. Using a for loop and a comparison operator
This method uses a simple for loop to iterate over the array and compare each element with the item using a comparison operator (such as ===). If the elements are equal, we can push the index to an array. This is a simple and straightforward approach that works for any type of array. Here’s an example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
function findAllIndexes(arr, val) { var indexes = [], i; for(i = 0; i < arr.length; i++) { if (arr[i] === val) { indexes.push(i); } } return indexes; } var arr = ["A", "B", "C", "A", "D", "A"]; var indexes = findAllIndexes(arr, "A"); console.log(indexes); // [0, 3, 5] |
3. Using reduce() function
This method uses the reduce() function to create an array of indexes from the original array. The reduce function takes a callback function that accumulates a value based on each element of the array. The callback function receives an accumulator, the current element, and the current index as parameters. In this case, the callback function checks if the element is equal to the item and pushes the index to an array. Here’s an example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
function findAllIndexes(arr, val) { var indexes = arr.reduce(function(acc, element, index) { if (element === val) { acc.push(index); } return acc; }, []); return indexes; } var arr = ["A", "B", "C", "A", "D", "A"]; var indexes = findAllIndexes(arr, "A"); console.log(indexes); // [0, 3, 5] |
4. Using forEach() function
This method uses the forEach() function to iterate over the array and execute a function for each element. The function can check if the element is equal to the item and push the index to an array. Here’s an example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
function findAllIndexes(arr, val) { var indexes = []; arr.forEach(function(element, index) { if (element === val) indexes.push(index); }); return indexes; } var arr = ["A", "B", "C", "A", "D", "A"]; var indexes = findAllIndexes(arr, "A"); console.log(indexes); // [0, 3, 5] |
5. Using map() and filter() functions
This method uses the map() and filter() functions to create an array of indexes from the original array. The map function creates a new array with the results of calling a function on every element of the array. In this case, the function returns the index if the element is equal to the item, or -1 otherwise. The filter function creates a new array with all elements that pass a test implemented by a function. In this case, the function filters out all elements that are -1. Here’s an example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
function findAllIndexes(arr, val) { var indexes = arr.map(function(element, index) { return element === val ? index : -1; }).filter(function(element) { return element !== -1; }); return indexes; } var arr = ["A", "B", "C", "A", "D", "A"]; var indexes = findAllIndexes(arr, "A"); console.log(indexes); // [0, 3, 5] |
In this example, arr.map() creates a new array where each element is either the index of the corresponding element in arr or -1 if it doesn’t match val. Then, .filter() removes all -1 values from the array, leaving only the indexes of the occurrences of value.
These are some of the ways to find the indexes of all occurrences of an item in an array in JavaScript. We can choose any of them based on our preference and use case.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)