- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2023 06:03 AM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2023 08:07 AM
var Array = ['apple', 'lemon', 'orange', 'kiwi', 'banana'];
gs.info(Array[4]);
Results:
*** Script: banana
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2023 08:07 AM
var Array = ['apple', 'lemon', 'orange', 'kiwi', 'banana'];
gs.info(Array[4]);
Results:
*** Script: banana
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2023 10:12 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2023 08:44 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-06-2023 10:15 AM
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!!