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:50 PM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
01-14-2021 10:47 AM
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.