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!
Solved! Go to Solution.
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
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
}
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
Hi Johannes!
It worked, thanks so much😊