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
Solved! Go to Solution.
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
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.
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
I tried both suggestions but I just get "{}"
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.
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.
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)
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
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