How to use slice/splice to extract elements from an Array
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2021 05:31 PM
HI ,
I have an array of string and when i use slice to extract sub set of elements proper results aren't returned.This code lies in a script action inside scoped application.However same code works fine when I execute from fix script in scoped application or from background script but does not work during the actual flow and I am not sure whats happening.
var userArray = ["njan","sam", "dyel","dbri","spatn","shra","kir","oln","ake"];
var extractedArray = userArray.splice(0,3);
extractedArray when executed in a fix script returns me - njan,sam,dyel but same code run inside script action returns me [".
I am not sure what the problem is.Appreciate any help/insights.
Thanks,
Neeraja
- Labels:
-
Scripting and Coding
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2021 07:27 PM
was the flow create under the context of the custom scope app?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2021 07:47 PM
The Actual flow is basically this code within script action which is triggered by an event and it is inside the scoped app.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-13-2021 09:21 PM
since you want to get 1st 3 array elements you can simply use userArray[0], userArray[1], userArray[2]
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-14-2021 10:45 AM
Hi Ankur,
Appreciate your response but got this working.The number of elements here is 3(as an example) but that would be a dynamic number hence its already running in a loop.
Thanks,
Neeraja.