How to use slice/splice to extract elements from an Array

Neeraja Janardh
Tera Contributor

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

 

6 REPLIES 6

Sukraj Raikhraj
Kilo Sage

was the flow create under the context of the custom scope app? 

The Actual flow is basically this code within script action which is triggered by an event and it is inside the scoped app.

@Neeraja Janardhan 

since you want to get 1st 3 array elements you can simply use userArray[0], userArray[1], userArray[2]

Regards
Ankur

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

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.