Trying to do some looping in Arcade label expression in Pro.
First, the following script works fine on a hosted feature layer, but on a feature class in a file geodatabase, it only returns the first field (ObjectID).
function countAnalyte(){
var allFields = []
for (var j in $feature){
Push(allFields, j)
}
return allFields
}
Second, I want to iterate through fields and build field names dynamically. I have a feature class with columns: AName1, AResult1, AName2, AResult2. The feature class may have unspecified numbers of these repeats. I want to build it so I can iterate on the number, and use each set of "AName" and "AResult" fields and their values to build the label expression. Something like this:
var acount = [1,2,3]
for (var i in acount){
$feature.AName+i //use value for exp
$feature.AResult+i //use value for exp
}
@XanderBakker any help is greatly appreciated!
Solved! Go to Solution.
That stinks! What if you use the Expects($feature,'*') and then loop over the feature, does that return all the fields? I would caution against this if it works as the labeling engine will returning a full row.
Hmmm, interesting. I'm still on 2.8, so I can't test it.
@sofoo Assuming you use Pro 2.9 or later, Does this label expression work for feature classes?
Expects($feature, "*")
var allFields = []
for (var j in $feature){
Push(allFields, j)
}
return allFields
First, the following script works fine on a hosted feature layer, but on a feature class in a file geodatabase, it only returns the first field (ObjectID).
Confirmed. The expression works for Feature Services, but not for features from a FGDB or EGDB (Microsoft Server). You can access the fields by using $feature.Field, but only OBJECTID abd GlobalID are available in the attributes dictionary:
This seems like a bug.
What about using the Schema function?
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
Schema() isn't available in the Labeling profile.
That stinks! What if you use the Expects($feature,'*') and then loop over the feature, does that return all the fields? I would caution against this if it works as the labeling engine will returning a full row.
Hmmm, interesting. I'm still on 2.8, so I can't test it.
@sofoo Assuming you use Pro 2.9 or later, Does this label expression work for feature classes?
Expects($feature, "*")
var allFields = []
for (var j in $feature){
Push(allFields, j)
}
return allFields
@MikeMillerGIS @JohannesLindner That's exactly what i needed! Thank you! They keep saying all functions in Pro are available in all other parts of the Enterprise but I keep running into functions that don't work with label expressions...
Also I am in Pro 2.9.3.
Well, "they" are kinda wrong.
There are multiple contexts in which you can use Arcade, so called "profiles". For example Labeling, Attribute Rules, Popups.
Different profiles have different globals and functions they can access. This is often done for performance reasons. Eg its's OK to load a feature layer and intersect it with a feature if you are working with a popup. But you can't do that in the Labeling profile, because it would be too slow to do the same operation for all features in the map extent.
You can find a list of the different profiles here: Profiles | ArcGIS Arcade | ArcGIS Developers
If a function is only available to some profiles, it is explicitly stated in the Function Reference: Function Reference | ArcGIS Arcade | ArcGIS Developers