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

Sourav16
Kilo Guru

Hi,

For extracting elements for a given range in an array you could use slice() instead of splice().

eg :

var userArray =  ["njan","sam", "dyel","dbri","spatn","shra","kir","oln","ake"];

var extractedArray = userArray.slice(0,3);

 

Thanks

Sourav

Hi Sourav,

Appreciate your response. Here I wanted to extract elements from the original array but also delete the extracted elements from original array after i extract into a sub array for which splice works more suitably.

 

Thanks,

Neeraja.