Find All Occurrence Indices in a JavaScript Array Last Updated : 25 Jan, 2025 Comments Improve Suggest changes Like Article Like Report Here are the different methods to find the index of all occurrences of a specific element in an array 1. Using While LoopUse a while loop to iterate through the array, and if an element matches the target, push its index into the array. Finally, return the array of indexes. JavaScript let a = ["GFG", "Geeks", "Portal", "Computer Science", "GFG", "GFG", "Geek"]; let elm = "GFG"; let ind = [], i = -1; while ((i = a.indexOf(elm, i + 1)) !== -1) { ind.push(i); } console.log(ind); Output[ 0, 4, 5 ] In this exampleThe while loop iterates through the array, finding the index of all element occurrences using indexOf().The result is stored in the ind array, which holds the indices of each occurrence of elm in a. 2. Using reduce() MethodTo find all indices of a specified element using reduce(), iterate through the array and push the index into a result array if the element matches the given value. Finally, return the array of indices. JavaScript let a = ["GFG", "Geeks", "Portal", "Computer Science", "GFG", "GFG", "Geek"]; let elm = "GFG"; let index = a.reduce((ind, el, i) => { if (el === elm) ind.push(i); return ind; }, []); console.log(index); Output[ 0, 4, 5 ] In this examplereduce(): Iterates through the array, pushing the index i into the ind array whenever the element el matches elm.The result is directly logged.3. Using forEach() MethodThe arr.forEach() method calls the provided function once for each element of the array. JavaScript let a = [1, 2, 3, 2, 4, 2, 5]; let tar = 2; let ind = []; a.forEach((val, index) => { if (val === tar) ind.push(index); }); console.log(ind); Output[ 1, 3, 5 ] In this exampleforEach(): Loops through the array and pushes the index into the ind array whenever the value matches tar.The result is directly logged.4. Using map() and filter() MethodThe map() and filter() methods to find the index of all occurrences of elements in an array using JavaScript. JavaScript let a = ["GFG", "Geeks", "Portal", "Computer Science", "GFG", "GFG", "Geek"]; let elm = "GFG"; let arr = a .map((element, index) => (element === elm ? index : -1)) .filter((index) => index !== -1); console.log(arr); Output[ 0, 4, 5 ] In this examplemap(): Creates an array of indices where the element equals elm or -1 if it doesn't match.filter(): Removes -1 values, leaving only the indices where elm was found.6. Using for...of Loop with entries() MethodUse the for...of loop in combination with the entries() method to iterate over the array with both the index and value. If the value matches the target, we push the index to the results array. JavaScript let a = [1, 2, 3, 2, 4, 2, 5]; let tar = 2; let ind = []; for (const [index, val] of a.entries()) { if (val === tar) ind.push(index); } console.log(ind); Output[ 1, 3, 5 ] In this examplefor...of with entries(): Iterates through the array, giving both the index and value. If the value matches tar, the index is pushed into ind.The result is logged directly. Comment More infoAdvertise with us Next Article Find All Occurrence Indices in a JavaScript Array P PranchalKatiyar Follow Improve Article Tags : JavaScript Web Technologies javascript-array JavaScript-DSA Similar Reads How to Add Elements to a JavaScript Array? Here are different ways to add elements to an array in JavaScript.1. Using push() MethodThe push() method adds one or more elements to the end of an array and returns the new length of the array.Syntaxarray.push( element1, element2, . . ., elementN );JavaScriptconst arr = [10, 20, 30, 40]; arr.push( 3 min read Create an Array of Given Size in JavaScript The basic method to create an array is by using the Array constructor. We can initialize an array of certain length just by passing a single integer argument to the JavaScript array constructor. This will create an array of the given size with undefined values.Syntaxconst arr = new Array( length );J 3 min read Insert at the Beginning of an Array in JavaScript Following are different ways to add new elements at the beginning of an array1. Using the Array unshift() Method - Most Used:Adding new elements at the beginning of the existing array can be done by using the Array unshift() method. This method is similar to the push() method but it adds an element 2 min read Remove Elements From a JavaScript Array Here are the various methods to remove elements from a JavaScript ArrayRemove elements from Array1. Using pop() methodThe pop() method removes and returns the last element of an array. This function decreases the length of the array by 1 every time the element is removed.javascriptlet a = ["Apple", 6 min read Remove Duplicate Elements from JavaScript Array To Remove the elements from an array we can use the JavaScript set method. Removing duplicate elements requires checking if the element is present more than one time in the array. 1. Using JavaScript Set() - Mostly UsedThe JavaScript Set() method creates an object containing only unique values. To r 3 min read How to Remove Multiple Elements from Array in JavaScript? Here are various methods to remove multiple elements from an array in JavaScript1. Using filter() MethodThe filter() method creates a new array with the elements that pass the condition. This method does not change the original array.JavaScriptlet a = [1, 2, 3, 4, 5]; let remove = [2, 4]; a = a.filt 3 min read Insert at the Beginning of an Array in JavaScript Following are different ways to add new elements at the beginning of an array1. Using the Array unshift() Method - Most Used:Adding new elements at the beginning of the existing array can be done by using the Array unshift() method. This method is similar to the push() method but it adds an element 2 min read Reverse an Array in JavaScript Here are the different methods to reverse an array in JavaScript1. Using the reverse() MethodJavaScript provides a built-in array method called reverse() that reverses the elements of the array in place. This method mutates the original array and returns the reversed array.JavaScriptlet a = [1, 2, 3 3 min read JavaScript - Delete last Occurrence from JS Array These are the following ways to remove the last Item from the given array: 1. Using pop() Method (Simple and Easiest Method for Any Array) The pop() method is used to get the last element of the given array it can also be used to remove the last element from the given array. JavaScriptlet a = [34, 2 2 min read Remove Empty Elements from an Array in JavaScript Here are different approaches to remove empty elements from an Array in JavaScript.1. Using array.filter() MethodThe array filter() method is used to create a new array from a given array consisting of elements that satisfy given conditions.array.filter( callback( element, index, arr ), thisValue )J 3 min read Like