How to find the position of an element in an array?

Not applicable

How to find the position of an element in an array?

Example:

var array = [1,2,3,4,5,6,7,8,9,10,11];

I have to find the position of element '5' in above array.

1 ACCEPTED SOLUTION

Mohith Devatte
Tera Sage
Tera Sage

hello ,

you can try this 

var array= [1,2,3,4,5,6,7,8,9,10,11];

var position = array.indexOf(5);

Hope this helps 

Mark the answer correct if this helps you

 

View solution in original post

5 REPLIES 5

Mohith Devatte
Tera Sage
Tera Sage

hello ,

you can try this 

var array= [1,2,3,4,5,6,7,8,9,10,11];

var position = array.indexOf(5);

Hope this helps 

Mark the answer correct if this helps you

 

Sagar Pagar
Tera Patron

Try by

 

 gs.info(array.indexOf("5"));

 

Thanks,

Sagar Pagar 

The world works with ServiceNow

Harshad Wagh
Tera Guru

you can try indexOf and also it needs in following format.

var array = ['1','2','3','4','5','6','7','8','9','10','11'];

gs.info(array.indexOf("5"));

 

Harshad Wagh
Tera Guru

Try following.

var array = [1,2,3,4,5,6,7,8,9,10,11];

gs.info(array.indexOf(5));

 

as the element in your array are not strings so the passing value cannot be string to.