Arcade expression too long

1116
5
Jump to solution
03-29-2022 12:18 PM
Kristine-DanielaShepherdson
New Contributor II

I was trying to figure out a way to create an if clause to label some disposition features using the "company" attribute. Unfortunately, there isn't much space on the map so I had to truncate the field and use the first word (no abbreviation field in the data either). The problem is that sometimes, the company name's first word can be something really short: 2 or 3 letters. So, I made an expression that uses an if clause to display 2 words from the company name, if the first one is 3 characters or less.

I feel the expression might be a bit too long/complicated/"not elegant". Does anyone have a nicer way of doing this?

 

var A = Split($feature.company," ")[0]+" "

var B = Split($feature.company," ")[1]+ "\n"+$feature.disp_num

var C = Split($feature.company," ")[0]+ "\n"+$feature.disp_num

 

var lttr1 = $feature.company[0]

var lttr2 = $feature.company[1]

var lttr3 = $feature.company[2]

var lttr4 = $feature.company[3]

 

if (lttr1 ==" ")

A+B

else if (lttr2 ==" ")

A+B

else if (lttr3 ==" ")

A+B

else if (lttr4 ==" ")

A+B

else

C

0 Kudos
2 Solutions

Accepted Solutions
jcarlson
MVP Esteemed Contributor

Yes, there's certainly an 'elegant' way to come at this.

First, you should assign the output of Split to a variable so that you can access it, rather than calling the function repeatedly.

Secondly, we can use Count to get the length of a string, and use that to check for short words, rather than look at the values of individual letters.

// Create array from company name
var name_parts = Split($feature.company, ' ')

// Check length of first value. If > 3, just return the first word, otherwise, get the first two.
if (Count(name_parts[0]) > 3){
    return name_parts[0]
} else {
    return `${name_parts[0]}\n${name_parts[1]}`
}

Here's that expression, with a long first word:

jcarlson_1-1648585159370.png

And here it is with a short word:

jcarlson_2-1648585182977.png

 

- Josh Carlson
Kendall County GIS

View solution in original post

KimGarbade
Occasional Contributor III

This is shorter.... if it does what you want....

 

var A = Split($feature.Company,' ', -1, true)
iif(Count(A[0]) < 3, A[0] + " " + A[1] + "\n" + $feature.disp_num, A[0] + "\n" +$feature.disp_num)

 

View solution in original post

5 Replies
jcarlson
MVP Esteemed Contributor

Yes, there's certainly an 'elegant' way to come at this.

First, you should assign the output of Split to a variable so that you can access it, rather than calling the function repeatedly.

Secondly, we can use Count to get the length of a string, and use that to check for short words, rather than look at the values of individual letters.

// Create array from company name
var name_parts = Split($feature.company, ' ')

// Check length of first value. If > 3, just return the first word, otherwise, get the first two.
if (Count(name_parts[0]) > 3){
    return name_parts[0]
} else {
    return `${name_parts[0]}\n${name_parts[1]}`
}

Here's that expression, with a long first word:

jcarlson_1-1648585159370.png

And here it is with a short word:

jcarlson_2-1648585182977.png

 

- Josh Carlson
Kendall County GIS
Kristine-DanielaShepherdson
New Contributor II

hmmm... PEYTO PEYTO :))) I wish I can meme-it somehow ❤️

KristineDanielaShepherdson_0-1648698683288.png

 

Kristine-DanielaShepherdson
New Contributor II

Oki. This one worked well too. Just needed a bit of tinkering:


var name_parts = Split($feature.company, ' ')

if (Count(name_parts[0]) > 3){
return name_parts[0]
} else {
return `${name_parts[0]}\n${name_parts[1]}`+"\n"+$feature.disp_num}

0 Kudos
KimGarbade
Occasional Contributor III

This is shorter.... if it does what you want....

 

var A = Split($feature.Company,' ', -1, true)
iif(Count(A[0]) < 3, A[0] + " " + A[1] + "\n" + $feature.disp_num, A[0] + "\n" +$feature.disp_num)

 

Kristine-DanielaShepherdson
New Contributor II

Thank you!

0 Kudos