How to find the index of all occurrence of elements in an array using JavaScript ?
Last Updated :
13 Jul, 2023
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:
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.
Javascript
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));
|
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.
Javascript
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();
|
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:
Javascript
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();
|
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:
Javascript
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();
|
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:
Javascript
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();
|
Share your thoughts in the comments
Please Login to comment...