Select to view content in your preferred language

Arcade EVAL function - evaluate a string to retrieve field value

367
1
Jump to solution
08-08-2023 10:25 AM
Labels (1)
KevinMayall
Occasional Contributor III
I'm looking for a way to construct a field name in Arcade and retrieve its value.  Say you have a hosted feature layer with an attribute field named after each day of the week.  I can construct the field name as "$feature.insertDayHere" but that is just a string that I need to evaluate to get the value.  Does Arcade have any kind of EVAL function?
 
html = '<html table header stuff>';
var daynames = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]
for (var d in daynames) {
  var fieldname = '$feature.' + d;
  html += '<tr>' + d + ': ' + fieldname + '</tr>';
}
 
Kevin
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

When using a variable as a field name, you have to use bracket notation to get the attribute of that field. You can also use template literals to construct your strings. And when looping through an array, the variable is just the index. You have to get the value of the array at that index.

 

html = '<html table header stuff>';
var daynames = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]
for (var d in daynames) {
    html += `<tr>${daynames[d]}: ${$feature[daynames[d]]}</tr>`;
}

 

 

View solution in original post

1 Reply
KenBuja
MVP Esteemed Contributor

When using a variable as a field name, you have to use bracket notation to get the attribute of that field. You can also use template literals to construct your strings. And when looping through an array, the variable is just the index. You have to get the value of the array at that index.

 

html = '<html table header stuff>';
var daynames = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]
for (var d in daynames) {
    html += `<tr>${daynames[d]}: ${$feature[daynames[d]]}</tr>`;
}