JavaScript provides several methods to find the index of an element in an array. The most commonly used methods are indexOf
and findIndex
.
Let’s take a closer look at each of these methods and how they work.
1. indexOf
Method:
The indexOf
method is a built-in Javascript array method that returns the first index at which a given element can be found in an array. If the element is not present in the array, it returns -1.
The syntax of the indexOf
is as follows:
array.indexOf(elementToFind, startIndex);
where array
is the array in which you want to search for the element, elementToFind
is the element you are looking for, and startIndex
is an optional parameter that specifies the index to start searching from.
If startIndex
is not provided, the search starts from the beginning of the array.
Here’s an example of using indexOf
:
const array = [1, 2, 3, 4, 5];
const element = 3;
const index = array.indexOf(element);
console.log(index); // Output: 2
In this example, the indexOf
method is used to find the index of the element 3
in the array array
. Since 3
is present at index 2
in the array, the output is 2
.
It’s important to note that indexOf
only returns the index of the first occurrence of the element that satisfies the condition. If there are multiple occurrences of the element in the array, only the index of the first occurrence will be returned.
2. findIndex
Method:
The findIndex
method is another built-in JavaScript array method that returns the index of the first element in the array that satisfies a given condition. If no such element is found, it returns -1.
The syntax for findIndex
is as follows:
array.findIndex(callback(element, index, array));
where array
is the array in which you want to search for the element, and callback
is a callback function that specifies the condition. The callback
function takes three arguments: element
, which represents the current element being processed, index
, which represents the index of the current element, and array
, which represents the array on which the findIndex
method was called.
Here’s an example of using findIndex
:
const array = [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }, { id: 5 }];
const elementId = 3;
const index = array.findIndex((item) => item.id === elementId);
console.log(index); // Output: 2
In this example, the findIndex
method is used to find the index of the element whose id
property matches 3
in the array of objects array
. Since the element with id
equal to 3
is present at index 2
in the array, the output is 2
.
It’s worth noting that findIndex
returns the index of the first element that satisfies the condition. If there are multiple elements that satisfy the condition, only the index of the first occurrence will be returned.
3. Looping through the Array:
Another approach to find the index of an element in an array is to loop through the array manually and compare each element with the element you are looking for.
You can use a for loop, a forEach loop, or any other type of loop to loop through the array and compare each element with the element you are looking for.
Here’s an example using a for
loop:
const array = [1, 2, 3, 4, 5];
const element = 3;
let index = -1;
for (let i = 0; i < array.length; i++) {
if (array[i] === element) {
index = i;
break; // exit the loop once the element is found
}
}
console.log(index); // Output: 2
In this example, a for
loop is used to iterate through the array array
. The if
statement inside the loop checks if the current element at index i
is equal to the element 3
that we are looking for. If it is, the index i
is assigned to the index
variable, and the loop is exited using the break
statement.
Thank you for reading, and let’s have conversation with each other
Thank you for reading my article. Let’s have conversation on Twitter and LinkedIn by connecting.