arcade output the field name alias

869
5
Jump to solution
06-03-2021 11:59 AM
Labels (1)
AngeliaH
New Contributor II

Hello!  I am trying to return the alias field name rather the the actual field name for a pop-up in a hosted feature layer in Arcade.  Basically I am searching my feature layer for any field that has the value "Yes" and want to return a list of fields.   I am trying to use the schema function but keep getting a syntax error.  Does anyone have any ideas on what I am doing wrong?

// Create a featureset, my layer is called ParkRecreationFacilities
var features = FeatureSetByName($map,"Park_and_Recreation_Facilities_public - Park and Recreation Facilities", ['*'], false);

// Create a dictionary holding the schema
var aDict = Schema(features);

// Create an array of dictionary's holding field info
var aArray = aDict["fields"];

// Access dictionary directly for field position 1
var aDict2 = aArray;

//Create output variable that will pull alias field name
var output = aDict2["alias"];

var returnstring = "Yes"
for (var i in $feature) {
     if ($feature[i] == returnstring) {
          output += i + TextFormatting.NewLine;
     }
}
return output

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Honored Contributor

What's happening is your var output line is failing since it's not a dictionary, but an array of dictionaries. You have to cycle through each of them to get the correct alias.

Here's one way to show the alias

 

var features = FeatureSetByName($map, 'your layer', ['*'], false);
var aDict = Schema(features);
var aArray = aDict["fields"];
var output;
for (var i in $feature){
  if ($feature[i] == 0) {
    for(var j in aArray) {
      var dict = aArray[j];
      if (dict['name'] == i){
        //console(dict['alias']);
        output += dict['alias'] + TextFormatting.NewLine;
      }
    }
  }
}
return output;<code></code><code></code>

 

Note: ignore the code stuff after "return output:". Something odd is going on with the parser.

 

View solution in original post

0 Kudos
5 Replies
AngeliaH
New Contributor II

I've also tried this syntax:

var output = ''
var returnstring = "Yes"
for (var i in $feature) {
    if ($feature[i] == returnstring) {
        output += Schema(i)["alias"] + TextFormatting.NewLine;
    }
}
return output

I feel like I am so close to figuring it out...

0 Kudos
KenBuja
MVP Honored Contributor

What's happening is your var output line is failing since it's not a dictionary, but an array of dictionaries. You have to cycle through each of them to get the correct alias.

Here's one way to show the alias

 

var features = FeatureSetByName($map, 'your layer', ['*'], false);
var aDict = Schema(features);
var aArray = aDict["fields"];
var output;
for (var i in $feature){
  if ($feature[i] == 0) {
    for(var j in aArray) {
      var dict = aArray[j];
      if (dict['name'] == i){
        //console(dict['alias']);
        output += dict['alias'] + TextFormatting.NewLine;
      }
    }
  }
}
return output;<code></code><code></code>

 

Note: ignore the code stuff after "return output:". Something odd is going on with the parser.

 

0 Kudos
AngeliaH
New Contributor II

Yes!  Thank you!  I had to tweak 1 little thing and it works!  Hooray!  Here is the final syntax:

var features = FeatureSetByName($map, 'Park_and_Recreation_Facilities_public - Park and Recreation Facilities', ['*'], false);
var aDict = Schema(features);
var aArray = aDict["fields"];
var output;
for (var i in $feature){
    if ($feature[i] == "Yes") {
        for(var j in aArray) {
            var dict = aArray[j];
            if (dict['name'] == i){
                output += dict['alias'] + TextFormatting.NewLine;
            }
        }
    }
}
return output;

0 Kudos
KenBuja
MVP Honored Contributor

Great! That one tweak was left over from testing with my data.

0 Kudos
BrunoRochaPA
New Contributor

Hi everyone,

I'm not an expert with Arcade I'm looking for help to slove a problem

So I'm using this code to hide on popups the null fields is it working but I would like to change the "field name" to the "field alias"
This is a related table but i think there's no problem with that.

Thank you all! :D

 

var skipFields = ['Creator', 'CreationDate', 'Editor', 'EditDate', 'OBJECTID', 'GlobalID'];
var allFields = '';
for(var i in $feature){
    var skip = False;
    for(var j in skipFields){
        if(Text(i) == Text(skipFields[j])){
            skip = True;
        }
    }

    if(isEmpty($feature[i])){
        Console(Concatenate('Null Field: ', i));
    } else if (skip){
        Console(Concatenate('Skipping Field: ', i));
    } else {
        Console(Concatenate('Including Field: ', i));
        allFields = Concatenate([allFields, Upper(i), TextFormatting.NewLine, Text($feature[i]), TextFormatting.NewLine, TextFormatting.NewLine]);
    }
}

Console(allFields);
return allFields;

 

 

0 Kudos