Listing polygon names that another polygon intersects

591
3
Jump to solution
09-15-2022 01:34 PM
Leahr
by
New Contributor II

Hello! I am currently writing an expression to list all of the cities (or just a single city) that a park area is in. I was able to get a loop to find and list all of the cities/city returned, just formatting it is my last issue. 

Currently what is returned is:

list.PNG

or 

single list.PNG

which depends on how many cities the park is in. (here is the expression)

code.PNG

Ideally, I would want the list to look more like

"This park is in: City1, City2, and City3"

or "This park is in: City1 and City2"

or "This park is in: City1"

(obviously, it would depend on the number of cities the park is in)

Does anyone have a solution to adding commas between city names, while not having a comma after the last city, but having an and before the last one (if possible)?

 

Thanks!

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

Here's another way:

var cityList = []
for(var c in inCity) {
    Push(cityList, c.CTU_NAME)
}
if(Count(cityList) > 1) {
    cityList[-1] = "and " + cityList[-1]
}
var cityString = Concatenate(cityList, ", ")
cityString = Replace(cityString, ", and", " and")
return "This park is in: " + cityString

Have a great day!
Johannes

View solution in original post

0 Kudos
3 Replies
KenBuja
MVP Esteemed Contributor

Here's one way to do that

 

var textArray = ['City 1', 'City 2', 'City 3'];
var output = 'This park is in: '
if (Count(textArray) == 1) return `${output} ${textArray[0]}`;
else if (Count(textArray) == 2) return `${output} ${Concatenate(textArray, ' and ')}`
else {
    for (var i in textArray) {
        if (i < count(textArray) - 1) output += `${textArray[i]}, `;
        else output += `and ${textArray[i]}`;
    }
    return output
}

 

0 Kudos
JohannesLindner
MVP Frequent Contributor

Here's another way:

var cityList = []
for(var c in inCity) {
    Push(cityList, c.CTU_NAME)
}
if(Count(cityList) > 1) {
    cityList[-1] = "and " + cityList[-1]
}
var cityString = Concatenate(cityList, ", ")
cityString = Replace(cityString, ", and", " and")
return "This park is in: " + cityString

Have a great day!
Johannes
0 Kudos
Leahr
by
New Contributor II

Hi Johannes!

It worked, thanks so much😊

0 Kudos