Jquery find key from a value

Asked

Viewed 65 times

2

I have an array with values that vary according to the page.

I’d like to know how to catch the key of a specific value.

Example:

minhaarray = [5,10,15,20,25]

How do I extract the key of a value 15?

1 answer

4


You n need jQuery for this, there is a native function of the Javascript prorpio that already does this, is the indexOf(), follows an example of how to use it

var minhaarray = [5,10,15,20,25];
var a = minhaarray.indexOf(15);

In this case it will return 2, which is the position that number 15 is in the array.

If you really want to use jQuery, there is a function of them called inArray(), which in this case you enter the value and the array and it returns the position it is in, e.g.:

var arr = [ 4, 5, 8, 9 ];
jQuery.inArray( 8, arr );

Obs: both the function indexOf() and inArray() return -1 if the value is not located inside the array

  • It worked very well the indexof, thank you very much for the light, hugging

Browser other questions tagged

You are not signed in. Login or sign up in order to post.