Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Find element in array

Lasse Korsgaar1
Tera Contributor

Hi,

 

I have a need in relation to arrays.

 

Ex.: I have an array that always consists of 5 elements, because I have limited my GlideRecord query to 5.

Array = [apple, lemon, orange, kiwi, banana]

I need to find the element in position 5 (banana) and store it in a variable.

 

I have found this thread, but it is the reverse activity - finding the position of the element.

Solved: How to find the position of an element in an array... - ServiceNow Community

 

Bring your magic!

Thanks.

1 ACCEPTED SOLUTION

Bert_c1
Kilo Patron
var Array = ['apple', 'lemon', 'orange', 'kiwi', 'banana'];
gs.info(Array[4]);

Results:
*** Script: banana

View solution in original post

5 REPLIES 5

Bert_c1
Kilo Patron
var Array = ['apple', 'lemon', 'orange', 'kiwi', 'banana'];
gs.info(Array[4]);

Results:
*** Script: banana

Hi @Lasse Korsgaar1 

 

you can try this 

var Array = [apple, lemon, orange, kiwi, banana]

var data = Array.indexOf(5);//you are storing this in a variable called data

 

Hope it helps

 

Thanks and Regards

Sure Sai Venkatesh

Lakshmiprasann7
Tera Contributor

 

 


If you want to populate last element try like this as below;
var arr= ['apple', 'lemon', 'orange', 'kiwi', 'banana'];

var ele=arr.pop();

gs.log(ele);

Please mark if it is helpful

Lakshmiprasann7
Tera Contributor
Hi@Lasse Korsgaar1,

var arr = ['apple', 'lemon', 'orange', 'kiwi', 'banana'];
var element=arr.pop();
gs.log(element);

result:banana
Please mark this response as helpful!!

Thanks!!