The findIndex Method In JS: Hello everyone, welcome to the nkcoderz.com website. In this article we will going to discuss about the The findIndex Method In JS.
The findIndex
method in JavaScript is a method of the Array object that returns the index of the first element in an array that satisfies a provided testing function. If no elements pass the test, it returns -1.
The findIndex Method In JS
Here’s the syntax for using the findIndex
method:
array.findIndex(callback(element[, index[, array]])[, thisArg])
where:
callback
: A function to be run for each element in the array, taking three arguments:element
,index
, andarray
. Theelement
is the current element being processed in the array. Theindex
is the index of the current element, and thearray
is the arrayfindIndex
was called upon. Thecallback
function should returntrue
if the element is the one you are looking for, andfalse
otherwise.thisArg
(optional): Object to use asthis
when executing callback.
Here’s an example that demonstrates how to use the findIndex
method to find the index of an element in an array:
Code 1 For findIndex Method
const numbers = [5, 10, 15, 20, 25, 30];
const index = numbers.findIndex(number => number === 20);
console.log(index);
Output
3
In this example, the findIndex
method is used to find the index of the element with a value of 20 in the numbers
array. The result is logged to the console and it returns 3
, which is the index of the element with a value of 20 in the array.
The findIndex
method can be useful in many situations where you need to find the index of a specific element in an array. Here are a few more examples to illustrate its use:
Code 2 For findIndex Method
const fruits = ['apple', 'banana', 'cherry', 'strawberry'];
const index = fruits.findIndex(fruit => fruit === 'cherry');
console.log(index);
const users = [
{id: 1, name: 'John'},
{id: 2, name: 'Jane'},
{id: 3, name: 'Jim'}
];
const userIndex = users.findIndex(user => user.id === 2);
console.log(userIndex);
Output
2
1
In the first example, the findIndex
method is used to find the index of the element with the value of ‘cherry’ in the fruits
array. In the second example, the findIndex
method is used to find the index of the user with an id
of 2 in the users
array.
It’s worth noting that the findIndex
method only returns the index of the first element that satisfies the provided testing function. If there are multiple elements in the array that meet the criteria, only the index of the first one will be returned.
If you need to find all indices of elements that meet the criteria, you could use the filter
method in combination with the map
method to achieve this:
Code 3 For findIndex Method
const allIndices = fruits.reduce((indices, fruit, index) => {
if (fruit === 'cherry') {
indices.push(index);
}
return indices;
}, []);
console.log(allIndices);
Output
[2]
Conclusion
If you liked this post The findIndex Method In JS, then please share this with your friends and make sure to bookmark this website for more awesome content.