Select to view content in your preferred language

Create label from the nonNull fields in layer

624
9
Jump to solution
01-29-2025 09:28 AM
Labels (1)
LandonHarris
Emerging Contributor

I am trying to write an arcade expression to return a list of the fields and values where the value is not null. I am filtering for the field name starting with "com". So far all I get is a blank dictionary. Just to clarify this is to populate a field which I will use to create the label.

 

 

var pattern = "com"  // Substring to match field names starting with "com"
var result = {}
for (var field in $feature) {
    if (Left(field, Count(pattern)) == pattern && !IsEmpty($feature[field])) {
        result[field] = $feature[field]
    }
}
Console(result)
return result

 

 

 

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

Put those console lines between lines 4 and 5 so you can see how they're being evaluated for the if statement. Also, initialize "results" as a dictionary, not a null (line 2).

var pattern = "com"; // Substring to match field names starting with "com"
var result = {};
Expects($feature, "*");
for (var field in $feature) {
  console(Left(field, Count(pattern)), pattern, Left(field, Count(pattern)) == pattern, $feature[field])
  if (Left(field, Count(pattern)) == pattern && !IsEmpty($feature[field])) {
    result[field] = $feature[field];
  }
}
Console(result);
return Text(result);

This is an example of how I tested it in the Playground

Snag_225a71e.png

 

View solution in original post

0 Kudos
9 Replies
RPGIS
by MVP Regular Contributor
MVP Regular Contributor

Hi @LandonHarris,

Are you trying to return the dictionary as a string value or simply return the value. If you are trying to return a dictionary as a string then use the method below.

 

var pattern = "com"  // Substring to match field names starting with "com"
var result = Null
Expects( $feature , '*' )
for (var field in $feature) {
    if (Left( field , Count( pattern ) ) == pattern && !IsEmpty($feature[field])) {
        result[field] = $feature[field]
    }
}
Console(result)
return Text(result)

I believe your expression is also correct but might need to include 'Expects' in order to get all of the fields and values.

 

KenBuja
MVP Esteemed Contributor

This would give you an empty result if the fields start with a capital "C". "com" is not equivalent to "Com"

Check whether this gives you the result you're expecting

var pattern = "com"  // Substring to match field names starting with "com"
var result = {}
for (var field in $feature) {
  if (Lower(Left(field, Count(pattern))) == pattern && !IsEmpty($feature[field])) {
    result[field] = $feature[field]
  }
}
Console(result)
return result

 

0 Kudos
LandonHarris
Emerging Contributor

I tried both suggestions but I just get "{}"

0 Kudos
RPGIS
by MVP Regular Contributor
MVP Regular Contributor

The only thing that it could be is that those might field aliases possibly and not actual field names or it might be that your criteria simply does not have a match.

Can you include a screenshot of the fields to see exactly what the issue is. It is hard to troubleshoot exactly when nobody can see what your seeing? It just needs to be a small snippet of the fieldnames.

0 Kudos
LandonHarris
Emerging Contributor

LandonHarris_0-1738182951529.png

 

0 Kudos
RPGIS
by MVP Regular Contributor
MVP Regular Contributor

You mentioned you tried using the 'Expects' as it had been given, and it still returned a blank dictionary.

Can you run your code with console wherever there is a line of code that is running and paste a snippet of the results? You code looks fine, but it is strange that it doesn't return the fields you are looking for.

0 Kudos
LandonHarris
Emerging Contributor

So I ran it like this and I'm getting nothing in the Console output. What am I missing?

var pattern = "com"  // Substring to match field names starting with "com"
var result = null
Expects($feature , '*')
for (var field in $feature){
    if (Left(field , Count(pattern)) == pattern && !IsEmpty($feature[field])) {
        result[field] = $feature[field]
        Console(result)
        Console(field)
        Console(pattern)
    }
}
Console(result)
return Text(result)
0 Kudos
KenBuja
MVP Esteemed Contributor

Put those console lines between lines 4 and 5 so you can see how they're being evaluated for the if statement. Also, initialize "results" as a dictionary, not a null (line 2).

var pattern = "com"; // Substring to match field names starting with "com"
var result = {};
Expects($feature, "*");
for (var field in $feature) {
  console(Left(field, Count(pattern)), pattern, Left(field, Count(pattern)) == pattern, $feature[field])
  if (Left(field, Count(pattern)) == pattern && !IsEmpty($feature[field])) {
    result[field] = $feature[field];
  }
}
Console(result);
return Text(result);

This is an example of how I tested it in the Playground

Snag_225a71e.png

 

0 Kudos
RPGIS
by MVP Regular Contributor
MVP Regular Contributor

Thanks @KenBuja for catching the result = "null" and not dictionary. That was my bad.

I should have wrote it like this.

var pattern = "com"  // Substring to match field names starting with "com"
var result = Null
for (var field in $feature) {
  if (Lower(Left(field, Count(pattern))) == pattern && !IsEmpty($feature[field])) {
    result = Text(Dictionary( field , $feature[field] ))// Create dictionary and convert to text
  }
}
Console(result)
return result
0 Kudos