Jquery find the key from a value

2

I have an array with values that vary by page.

I'd like to know how to get the key for a specific value.

Example:

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

How do I extract the key from the value 15?

    
asked by anonymous 12.02.2017 / 19:54

1 answer

4

You do not need jQuery for this, there is a native javascript function that already does this, it's indexOf() , here is 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 number 15 is in the array.

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

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

Note: both the indexOf() and inArray() function returns -1 if the value is not found inside the array

    
12.02.2017 / 19:57