Given an array containing array elements and the task is to find all occurrences of an element from the array.
We are going to do that with the help of JavaScript`s following methods:
- Using While Loop
- Using reduce() Method
- Using forEach() Method
- Using map() and filter() Method
- Using reduce() Method
Approach 1: Using While Loop
Declare an empty array that stores the indexes of all occurrences of the array element. Visit the array elements one by one using a while loop and if the array elements match with the given element then push the index of the element into an array. After visiting each element of the array, return the array of indexes.
Example: This example implements the above approach.
let arr = [ 'GFG' , 'Geeks' , 'Portal' ,
'Computer Science' , 'GFG' ,
'GFG' , 'Geek'
]; let elm = 'GFG' ;
function getInd(arr, val) {
let index = [], i = -1;
while ((i = arr.indexOf(val, i + 1)) != -1) {
index.push(i);
}
return index;
} console.log(getInd(arr, elm)); |
[ 0, 4, 5 ]
Approach 2: Using reduce() Method
Visit the array elements one by one using reduce() method and if the array elements match 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 occurrences of elements in an array.
let arr = [ 'GFG' , 'Geeks' , 'Portal' ,
'Computer Science' , 'GFG' ,
'GFG' , 'Geek'
]; let elm = 'GFG' ;
function GFG_Fun() {
let index = arr.reduce( function (ind, el, i) {
if (el === 'GFG' )
ind.push(i);
return ind;
}, []);
console.log(index);
} GFG_Fun(); |
[ 0, 4, 5 ]
Approach 3: Using forEach() Method
The arr.forEach() method calls the provided function once for each element of the array. The provided function may perform any kind of operation on the elements of the given array.
Example:
let arr = [ 'GFG' , 'Geeks' , 'Portal' ,
'Computer Science' , 'GFG' ,
'GFG' , 'Geek'
]; let elm = 'GFG' ;
const indexArr =[]; function GFG_Fun() {
arr.forEach((element, index) => {
if (element === elm) {
indexArr.push(index);
}
});
console.log(indexArr);
} GFG_Fun(); |
[ 0, 4, 5 ]
Approach 4: Using map() and filter() Method
In this approach, we can use map() and filter() methods to find the index of all occurrences of elements in an array using JavaScript.
Example:
let arr = [ 'GFG' , 'Geeks' , 'Portal' ,
'Computer Science' , 'GFG' ,
'GFG' , 'Geek'
]; let elm = 'GFG' ;
function GFG_Fun() {
const indexArr = arr.map((element, index) =>
(element === elm ? index : -1))
.filter(element => element !== -1);
console.log(indexArr);
} GFG_Fun(); |
[ 0, 4, 5 ]
Approach 5: Using reduce() Method
The Javascript arr.reduce() method in JavaScript is used to reduce the array to a single value and executes a provided function for each value of the array (from left to right) and the return value of the function is stored in an accumulator.
Example:
let arr = [ 'GFG' , 'Geeks' , 'Portal' ,
'Computer Science' , 'GFG' ,
'GFG' , 'Geek'
]; let elm = 'GFG' ;
const indexArr =[]; function GFG_Fun() {
arr.reduce( function (arr,ele,ind){
if (ele===elm){
indexArr.push(ind);
}
},[]);
console.log(indexArr);
} GFG_Fun(); |
[ 0, 4, 5 ]