JavaScript ES6 by Example — Array.find() & Array.findIndex()
In this post we will give you information about JavaScript ES6 by Example — Array.find() & Array.findIndex(). Hear we will give you detail about JavaScript ES6 by Example — Array.find() & Array.findIndex()And how to use it also give you demo for it if it is necessary.
Array.find()
and Array.findIndex()
are two new methods in JavaScript ES6. They make searching arrays more easier than before without writing cumbersome code and loops.
Let’s see them in more details with simple examples.
The Array.find()
Method by Example
The Array.find()
method is used to search for the first element in the array that verifies the passed predicate.
Let’s take this simple example of an array of numbers:
letmyArr=[9,3,2,4,9,11,19];myArr.find((v)=>{returnv%2===;});
In this example, we are looking for the first even number (can be divided by zero) in the array. In this case, this will return 2.
the find()
method calls the (v)=>{ return v % 2 === 0;}
predicate for each element in the array until the predicate returns true. In this case the element is returned. If no element is found, undefined
is returned.
Note: A predicate is simply a function that returns a boolean value i.e true or false.
The Array.findIndex()
Method by Example
The findIndex()
method is quite similar to the find()
method but returns the index of the element instead of the element itself.
It takes a predicate and returns the index of the first element in the array that satisfies the predicate. If no element exists -1
is returned.
Let’s take the previous example:
letmyArr=[9,3,9,4,9,11,19];letind=myArr.findIndex((v)=>{returnv%2===;});if(ind!==-1){console.log("There exists at least one even number in the array: ",myArr[ind])}
Instead of returning 4, the findIndex()
method returns its index which is 3.
We save the index in the ind
variable then we check if it’s different than -1
. In this case, we display There exists at least one even number in the array: in the console along with the element corresponding to the returned index.
Wrap-up
We’ve seen examples of using ES6 Array.find()
and Array.findIndex()
methods which provide easy ways to search for an element in JavaScript arrays.
Hope this code and post will helped you for implement JavaScript ES6 by Example — Array.find() & Array.findIndex(). if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs