# Listing polygon names that another polygon intersects

628
3
09-15-2022 01:34 PM
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:

or

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

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!

Tags (6)
1 Solution

Accepted Solutions
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
3 Replies
by
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
}``````

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
by
New Contributor II

Hi Johannes!

It worked, thanks so much😊