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
Solved! Go to Solution.
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:
And here it is with a short word:
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)
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:
And here it is with a short word:
hmmm... PEYTO PEYTO :))) I wish I can meme-it somehow ❤️
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}
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)
Thank you!