How to find index of element in array using jQuery

Today, for my project requirement, I need to find out index of any element in jQuery. I found the solution. jQuery provides a method called "inArray" which search for the specified value in the array and returns the index of the element.

Earlier I had posted about jQuery solution to remove item from array, split an array, combine/join arrays and remove duplicate items from array, And In this post, see how to find index of element in array with example.

Prototype of method
inArray("Value to search for", "Array in which value needs to be searched");

Below jQuery code snippets provides you an overview of how to use inArray() method.
$(document).ready(function(){
  var arrData = [ "j", "Q", "u", "e","r","y" ];
  alert(jQuery.inArray("Q", arrData));
});
When run, it will alert 1 as index starts from 0. Cool and simple.

See live Demo and Code.

Feel free to contact me for any help related to jQuery. I will gladly help you.
SHARE

Nothing shocks me, I'm a Software Engineer. And I am not young enough to know everything. I live in World Wide Web and from there take care of this website. This website communicates about my work, learning and experience. I believe life is short, and it is for loving, sharing, learning and connecting. So lets connect..