So I am following this blog, Introducing Data Expressions in ArcGIS Dashboards, to learn Arcade for use in Dashboards.
Can someone please explain these two pieces of codes line by line?
// Empty dictionary to capture each hazard reported as separate rows.
var choicesDict = {'fields': [{ 'name': 'split_choices', 'type': 'esriFieldTypeString'}],
'geometryType': '', 'features': []}; // Need help understanding the syntax
var index = 0;
// Split comma separated hazard types and store in dictionary.
for (var feature in fs) {
var split_array = Split(feature["Report_road_condition"], ',')
var count_arr = Count(split_array)
for(var i = 0; i < count_arr; i++ ){
choicesDict.features[index++] = {
'attributes': { 'split_choices': Trim(split_array[i]), // Don't know what this line is doing
}}
}}
To post code:
var choicesDict = {
'fields': [
{'name': 'split_choices', 'type': 'esriFieldTypeString'},
],
'geometryType': '',
'features': []
}
They are creating a Dictionary that is used to return the final Featureset. Featuresets are collections of features. They can be constructed from a Dictionary or from a JSON string (representing the dictionary).
A Featureset has to have the following keys:
In the snippet above, they are creating this structure. They are declaring the featureset to be a non-spatial table (geometryType = "") with one String field called split_choices. They leave the contained features empty. This is a common workflow. You create your dictionary and then fill in the features in a later step (see next snippet).
The second snippet uses an old workflow, I'm going to update it here:
for (var f in fs) {
var split_array = Split(f["Report_road_condition"], ',')
for(var i in split_arr){
var new_feature = {
'attributes': { 'split_choices': Trim(split_array[i]) }
}
Push(choicesDict.features, new_feature)
}
}
So in this step, they loop over each feature in the input featrueset, split a field value of that feature, and append each part to the output featureset. If the input featureset has one feature with the value "A, B, C", the output featureset will have three features with the values "A", "B", "C".
The last step in these workflows is to return the Dictionary as Featureset:
return Featureset(choicesDict)
It's also worth to note that there are two ways to address a dictionary key or field value (the example uses both, which is ugly...)