Arcade to return field names that contain a string

615
3
Jump to solution
08-31-2022 03:07 AM
Labels (1)
abrown8
New Contributor III

Hi all

I'm fairly new to Arcade and programming and I've got myself a bit stuck so would welcome any help!

In AGOL, I have a hosted feature layer that I'm using in a Dashboard. I want to add in indicators that do some calculations e.g. sum, minimum, maximum.

However, the dataset has a lot of fields that I don't really want to manually list out. The fields that will be used in the calculations all have field names that have the prefix "D_". I am able to create the result I want by listing the fields used in the calculation:

 

var fields = [
$datapoint["D_20210913"],
$datapoint["D_20210925"],
$datapoint["D_20211007"],
$datapoint["D_20211019"],
$datapoint["D_20211031"],
$datapoint["D_20211112"],
$datapoint["D_20211124"],
...
]
var minimum = min(fields)
var rounded = Round(minimum,3)

return {...}

 

What I'm wondering is, is it possible to use Find and a For loop etc. to list out all the fields that have field names containing "D_"?

@XanderBakker I'd appreciate any help 🙂

Many thanks!

Alice

0 Kudos
1 Solution

Accepted Solutions
MikeMillerGIS
Esri Frequent Contributor

$feature is iterable, which each iteration is a field.  This might help

* Edit, I think using the Schema function is safer, updated code to use it instead of looping over $feature

var fields = [];
var values = [];
var all_fields = Schema($feature)['fields'];
for (var i in all_fields){
    var field_dict = all_fields[i]
    var x = field_dict['name']
    Push(fields,x)
    push(values, $feature[x])
}
return values

 

 

View solution in original post

3 Replies
MikeMillerGIS
Esri Frequent Contributor

$feature is iterable, which each iteration is a field.  This might help

* Edit, I think using the Schema function is safer, updated code to use it instead of looping over $feature

var fields = [];
var values = [];
var all_fields = Schema($feature)['fields'];
for (var i in all_fields){
    var field_dict = all_fields[i]
    var x = field_dict['name']
    Push(fields,x)
    push(values, $feature[x])
}
return values

 

 

jcarlson
MVP Esteemed Contributor

Building on @MikeMillerGIS 's excellent Schema method to subset your fields for those with "D_" in the name, include something like this in the main for loop:

// other stuff above...
    var x = field_dict['name']
    if (Left(x, 2) == 'D_'){    
        Push(fields,x) 
        Push(values, $feature[x])
    }
}

return min(values)

 

- Josh Carlson
Kendall County GIS
abrown8
New Contributor III

@MikeMillerGIS , @jcarlson Thank you for your help, it's greatly appreciated! That's worked wonders.

I wasn't aware of the Push function so I've learned something new

0 Kudos