All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

JavaScript Find Index Of Object In Array

Last Updated : Mar 11, 2024

JavaScript Find Index Of Object In Array

In this tutorial we will show you the solution of JavaScript find index of object in array, here we used findIndex() method, its executes a function for each array element and returns index of the first element from that array.

If no match found then it returns -1 as result we can also use methods of indexOf or find like in javascript.

Step By Step Guide On JavaScript Find Index Of Object In Array :-

Here we defined an array ‘arr’ with numeric values ‘[20,30,7,4,89]’ then we used findIndex method on that so we can find index of particular value from array ‘arr’.

Within findIndex method we need to give some condition for find that particular value so we checks which value from array is equal to ‘89’ then it will return its position from array.

We can also use greatest value from an array condition result will be same.

<!DOCTYPE html>
        <title>FIND INDEX IN ARRAY JS</title>
            const arr=[20,30,7,4,89];
            const ind=arr.findIndex(arrval=>arrval === 89);
            console.log("Array Index Value Is",ind);
  1. <!DOCTYPE html> tag which is instruct the web browser about what version of HTML file written in and it’s not have any ending tag.
  2. The<html> tag is used to indicate the beginning of HTML document.
  3. As above shown <head> tag is contains information about webpage and external file links are declared here. <title> tag is used for set the webpage title.
  4. Both <head> and <title> tags having their pair end tag, so we need to close the ending tags respectively. If you’re not closed anyone of ending tag properly that is also affect the webpage result.
  5. <body> tag is beginning of main coding part because it contain coding of entire website blocks and elements described here.
  6. Here we defined an array variable ‘arr’ with values ‘[20,30,7,4,89]’ then we defined another variable ‘ind’ for store index of found value from array.
  7. Within findIndex method we need to use some variable for iterate purpose so we defined ‘arrval’ variable then this method needs to appends with array ‘arr’.
  8. Then ‘arrval’ will iterate each value in array with condition equal to value ‘89’ which is matched so we finally get result ‘4’ and displayed on console.
  9. Both </body>,</html> tags closed respectively. </body> tag indicates the end of body, Then </html> tag indicates the end of HTML document.

Conclusion :-

In conclusion now we are able to know find index of object in array using javascript.

When we executes our program on browser we can’t see anything because our result is printed on console panel.

For saw result we need to use shortcut ‘ctrl+shift+j’ then console panel will open with result ‘Array Index Value Is 4’.

When we using ‘console.log()’ method for display we need to open console panel otherwise we can’t see result.

I hope this tutorial on JavaScript find index of object in array helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪