How to find the index of all occurrence of elements in an array using JavaScript ? - GeeksforGeeks

20 Nov, 2019
Given an array containing array elements and the task is to find all occurrence of an element from the array. We are going to do that with the help of JavaScript.
Approach 1: Declare an empty array which stores the indexes of all occurrence of the array element. Visit the array elements one by one using while loop and if the array elements match with the given element then push the index of element into an array. After visiting each element of the array, return the array of indexes.
Example: This example implements the above approach.
<!DOCTYPE HTML> <html>   <head>     <title>         How to find the index of all occurrences        of element in an array using JavaScript?    </title></head>   <body style = "text-align:center;">           <h1 style = "color:green;" >         GeeksForGeeks     </h1>          <p id = "GFG_UP" style =        "font-size: 15px; font-weight: bold;">    </p>          <button onclick = "GFG_Fun()">        Click Here    </button>          <p id = "GFG_DOWN" style =        "color:green; font-size: 20px; font-weight: bold;">    </p>          <script>        var up = document.getElementById('GFG_UP');         var down = document.getElementById('GFG_DOWN');                   var arr = [            'GFG', 'Geeks', 'Portal',             'Computer Science', 'GFG',             'GFG', 'Geek'        ];                  var elm = 'GFG';                  up.innerHTML = "Click on the button to find "                    + "the position of element in "                    + "array.<br><br>Array = ["                     + arr + "]<br>" + "Element - '"                    + elm + "'";                  function getInd(arr, val) {            var index = [], i = -1;            while ((i = arr.indexOf(val, i+1)) != -1){                index.push(i);            }            return index;        }              function GFG_Fun() {            down.innerHTML = getInd(arr, elm);        }     </script> </body>   </html>
Output:
Before clicking on the button:
After clicking on the button:
Approach 2: Visit the array elements one by one using reduce() method and if the array elements matches with the given element then push the index into the array. After visiting each element of the array, return the array of indexes.
Example: This example uses reduce() method to find the index of all occurrence of elements in an array.
<!DOCTYPE HTML> <html>   <head>     <title>         How to find the index of all occurrences        of element in an array using JavaScript?    </title></head>   <body style = "text-align:center;">           <h1 style = "color:green;" >         GeeksForGeeks     </h1>          <p id = "GFG_UP" style =         "font-size: 15px; font-weight: bold;">    </p>          <button onclick = "GFG_Fun()">        Click Here    </button>          <p id = "GFG_DOWN" style =         "color:green; font-size: 20px; font-weight: bold;">    </p>          <script>        var up = document.getElementById('GFG_UP');         var down = document.getElementById('GFG_DOWN');                   var arr = [            'GFG', 'Geeks', 'Portal',            'Computer Science', 'GFG',            'GFG', 'Geek'        ];                  var elm = 'GFG';                  up.innerHTML = "Click on the button to "                + "find the position of element"                + " in array.<br><br>Array = ["                 + arr + "]<br>" + "Element - '"                + elm + "'";                  function GFG_Fun() {            down.innerHTML =                 arr.reduce(function(ind, el, i) {                    if (el === 'GFG')                        ind.push(i);                    return ind;                }, []);         }     </script> </body>   </html>
Output:
Before clicking on the button:
After clicking on the button:
nidhi_biet
javascript-array
JavaScript
Web Technologies
Web technologies Questions
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Remove elements from a JavaScript Array
Convert a string to an integer in JavaScript
Difference between var, let and const keywords in JavaScript
Differences between Functional Components and Class Components in React
Difference Between PUT and PATCH Request
Remove elements from a JavaScript Array
Installation of Node.js on Linux
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?