Select to view content in your preferred language

Slice "step" possible with Arcade?

400
1
05-28-2024 03:09 PM
ColeSutton
Occasional Contributor

With python, I can slice an array or string using brackets and a colon. And using the optional "step" parameter I can get every second (or third, fourth, etc) array item. I just add a second colon followed by the number 2 (or 3, or 4, etc). For example:

 

#slicing with python
myString =  "Examples"
myString[0:6] #returns "Exampl"
myString[0:6:2] #returns "Eap"

 

 

With arcade, I know how to perform a basic string slice

 

//slicing with Arcade
var myString = "Examples"
Concatenate(Slice(Split(myString,""), 0, 6)) //returns "Exampl"

 

 

I'm not sure how to get every second letter with Arcade, like the python step parameter does. At least not in a single line.

 

var myString = "Examples"
//i know the below won't work, but is something like this possible?
Concatenate(Filter(Slice(Split(myString,""), 0, 6), index % 2 == 0)) 

 

 

I know I could set up a for loop to get the desired result, but I'm curious if it can be achieved in a single line without a For loop. Any ideas?

Tags (3)
0 Kudos
1 Reply
jcarlson
MVP Esteemed Contributor

If you throw that loop into a function, it's technically a one-liner when you actually execute it? Not great, though.

var myString = "Examples"

function Step(arr, step) {
  var out_array = []
  for (var a in arr) {
    if (a % step == 0) {
      Push(out_array, arr[a])
    }
  }
  return out_array
}

return Concatenate(Step(Slice(Split(myString, ""), 0, 6), 2))

jcarlson_0-1716938988467.png

 

- Josh Carlson
Kendall County GIS
0 Kudos