Hello! I am working on an attribute rule that will concatenate 6 fields that make up a street address to create a full address. The problem is that 2 of the fields are not always populated with a value. They will either be null or empty.
In the image below, you can see my 6 fields (ST_NUM, DIRPRE, FEANME, FEATYP, DIRSUF and Unit (LV_APT)
If I do a simple concatenate of those fields, the result will leave spaces at the end of the full address. I know this is because of there not always being a value in DIRSUF or LV_APT.
I have been working on the below code, but I know I am missing some parts. Any ideas or anyone know a better way?
// List the fields to combine
var parts = [$feature.ST_NUM, $feature.DIRPRE, $feature.FEANME, $feature.FEATYP, $feature.DIRSUF, $feature.LV_APT];
// Loop through the address parts, if not null or empty add them to a new array
var filteredparts = [];
for (var i in parts) {
var value = parts[i];
if (IsEmpty(value)) continue;
filteredparts[Count(filteredparts)] = value
}
// Concatenate with a space
return Concatenate(filteredparts, " ");
I think you have to add the "" function to check for if a part is empty.
// List the fields to combine
var parts = [$feature.ST_NUM, $feature.DIRPRE, $feature.FEANME, $feature.FEATYP, $feature.DIRSUF, $feature.LV_APT];
// Function to check if a value is null or empty
function IsNullOrEmpty(value) {
return value == null || value == "";
}
// Loop through the address parts, if not null or empty add them to a new array
var filteredparts = [];
for (var i in parts) {
var value = parts[i];
if (IsNullOrEmpty(value)) continue;
filteredparts[Count(filteredparts)] = value;
}
// Concatenate with a space
return Concatenate(filteredparts, " ");
Thanks @Brian_McLeer
I found this as well
var fullName
if(IsEmpty($feature.ST_NUM)==false){
fullName += $feature.ST_NUM + " "
}
if(IsEmpty($feature.DIRPRE)==false){
fullName += $feature.DIRPRE + " "
}
if(IsEmpty($feature.FEANME)==false){
fullName += $feature.FEANME + " "
}
if(IsEmpty($feature.FEATYP)==false){
fullName += $feature.FEATYP + " "
}
if(IsEmpty($feature.DIRSUF)==false){
fullName += $feature.DIRSUF + " "
}
if(IsEmpty($feature.LV_APT)==false){
fullName += $feature.LV_APT + " "
}
return fullName
Here is how we do it
function not_empty(x) {
return !IsEmpty(x);
}
return Concatenate(Filter(['a', 'b', '', null,'c'], not_empty), '-');
returns
"a-b-c"
If you want to handle " ", then change not_empty to
function not_empty(x) {
if (TypeOf(x) == 'String'){
x = Trim(x)
}
return !IsEmpty(x);
You may need to adapt your code filter out strings with spaces.
IsEmpty(null) // true
IsEmpty("") // true
IsEmpty(" ") // false