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:

Download  Run Code

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:

Download  Run Code

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:

Download  Run Code

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:

Download  Run Code

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:

Download  Run Code

 
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.