Getting $feature attribute using brackets and variable not working.

346
2
03-02-2022 01:02 AM
omar-marji
New Contributor III

Trying to access a feature's attribute using the bracket notation does not work when using a variable (unless it is accessed at least once using a string.

 

If you take a look at the snippet below. This gives an error

var fldName = "ASSETTYPE";
Console($feature[fldName]);

 

The error message is :

Invalid expression.
Error on line 2.
Field not found ASSETTYPE

 

While If I try to use the snippet below. It works without an error

var ftrValue = $feature["ASSETTYPE"];
Console(ftrValue);
var fldName = "ASSETTYPE";
Console($feature[fldName]);

 

Any ideas how I can work around this restriction? I have a dynamic list of fields that I am trying to use to get values from the feature.

 

One thing to note is that if I use OBJECTID for the first snippet, it does not give any errors and works without having to access the field first using an inline string!

 

0 Kudos
2 Replies
JohannesLindner
MVP Frequent Contributor

I'm sure there are more elegant solutions...

var attributes = Dictionary(Text($feature))["attributes"]

var field_names = ["ASSETTYPE", "OBJECTID"]
for(var i in field_names) {
  Console(attributes[field_names[i]])
}

Have a great day!
Johannes
omar-marji
New Contributor III

Thanks for your help.

Unfortunately, this still has the same issue and I am starting to think it is in the way the "verify" function works when creating the attribute rules.

Check the below scenario:

Console(Text($feature));

 

This results in the following object

{
  "geometry":{
    "x":####,
    "y":####,
    "z":0,
    "m":null,
    "spatialReference":{"wkid":####}
  },
  "attributes":{
    "GLOBALID":"{443094DA-F564-47D2-A07E-F47EA9B1C03A}",
    "OBJECTID":5
  }
}

 

While this scenario (although the attributes was accessed after the console call)

Console(Text($feature));
var assetType = $feature["ASSETTYPE"];

 

Results in the following

{
  "geometry":{
    "x":####,
    "y":####,
    "z":0,
    "m":null,
    "spatialReference":{"wkid":####}
  },
  "attributes":{
    "ASSETTYPE":101,
    "GLOBALID":"{443094DA-F564-47D2-A07E-F47EA9B1C03A}",
    "OBJECTID":5
  }
}
0 Kudos