How to slice array in Arcade?

4575
6
Jump to solution
07-09-2019 05:22 AM
Zeke
by
Regular Contributor III

I have an Arcade expression that I want to use for labeling. If the label field (named Label) is two words or less, I return the field. This works fine. But if the Label is more than two words, it should return the first two words, followed by a newline, then the rest of the words. In Python, this would be something like lbl_array[:2] + '\n' + lbl_array[2:]. But I can't figure out or find how to slice Arcade array elements like that. I could use a for loop to construct the label, but there should be a way to use the indexes.

var lbl = $feature["Label"]

var lbl_array = Split(lbl, ' ')

if (count(lbl_array) < 3) {

    return lbl }

else {

?? }

0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

Hi Greg Keith ,

You can use the code below, but since you mention that you are creating labels, you will not be able to use the NewLine constant. This will be supported when the new web map based on JavaScript API 4.x becomes available this fall:

var txt = "aaaa bb ccc d";
var arr = Split(txt, " ");
if (Count(arr)>2) {
    var arr_new = [arr[0], arr[1]];
    var result = Concatenate(arr_new, TextFormatting.NewLine);
    //var result = arr[0] + TextFormatting.NewLine + arr[1];
} else {
    var result = txt;
}

return result;

View solution in original post

6 Replies
XanderBakker
Esri Esteemed Contributor

Hi Greg Keith ,

You can use the code below, but since you mention that you are creating labels, you will not be able to use the NewLine constant. This will be supported when the new web map based on JavaScript API 4.x becomes available this fall:

var txt = "aaaa bb ccc d";
var arr = Split(txt, " ");
if (Count(arr)>2) {
    var arr_new = [arr[0], arr[1]];
    var result = Concatenate(arr_new, TextFormatting.NewLine);
    //var result = arr[0] + TextFormatting.NewLine + arr[1];
} else {
    var result = txt;
}

return result;
Zeke
by
Regular Contributor III

Ok, thanks. Found this on the Ideas page, upvoted it.

Zeke
by
Regular Contributor III

Xander Bakker

One thing though. Is it possible to use index slicing, like in Python, or do array elements need to be accessed individually? If the latter, it seems like a big drop in usefullness compared to using Python.

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi Greg Keith ,

Slicing (slice) is not a supported function in Arcade (yet?). When you compare Python with Arcade, Python has a lot more functionality and module you can plug in. However, Arcade has a different purpose. It is meant as a cross-platform language that can be used for things that are not possible to do with Python (like configuring a pop-up and cross referencing other data layer and dynamically do analysis to show the results in that pop-up). A lot of functionality is being developed and more profiles (places where you can apply Arcade) are being added. Although there is still a lot of work ahead for enhancements, Arcade is pretty cool as it is right now...

You will probably run into several limitations of Arcade like the NewLine that is not supported, but that specific case is not a limitation of Arcade. It is a limitation of the web map based on the JavaScript API 3.x. Later this year the web map will receive an upgrade to 4.x and that will remove that limitation.

Zeke
by
Regular Contributor III

Ok, thanks again! Will make do until fall, I guess.

KevinMayall
Occasional Contributor III

Arcade now has a Slice function.

 

Kevin
0 Kudos