<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Map Viewer Sytles with Arcade Expression: Working... but not actually in ArcGIS Online Questions</title>
    <link>https://community.esri.com/t5/arcgis-online-questions/map-viewer-styles-with-arcade-expression-working/m-p/1346219#M55693</link>
    <description>&lt;P&gt;Thank you Kristian, that is all very useful as well! Would you expect a performance benefit using the Reduce function instead of a for loop on larger datasets? I tried both ways on my data, but didn't see a noticeable difference (perhaps became it is relatively small).&lt;/P&gt;</description>
    <pubDate>Mon, 06 Nov 2023 17:30:55 GMT</pubDate>
    <dc:creator>AaronKoelker</dc:creator>
    <dc:date>2023-11-06T17:30:55Z</dc:date>
    <item>
      <title>Map Viewer Styles with Arcade Expression: Working... but not actually</title>
      <link>https://community.esri.com/t5/arcgis-online-questions/map-viewer-styles-with-arcade-expression-working/m-p/1345822#M55664</link>
      <description>&lt;P&gt;I have a hosted feature service of county boundaries within a web map:&amp;nbsp;&lt;A href="https://fgio.maps.arcgis.com/apps/mapviewer/index.html?webmap=c6d86a156e80458e8d4104d20c3900dc" target="_blank" rel="noopener"&gt;https://fgio.maps.arcgis.com/apps/mapviewer/index.html?webmap=c6d86a156e80458e8d4104d20c3900dc&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The feature service has several fields, each containing only Yes/No values. I want to stylize the layer by getting a count of how many instances of 'YES' there are across all attributes for each record. I wrote an Arcade expression within the Styles panel that correctly returns the number of instances as an integer. I also applied this expression to the pop-up, and can see the expected results for each feature when I click it. However, when I go to actually style the layer (Counts and Amounts (color) for example), the map won't draw using the results of the Arcade expression. It doesn't draw anything at all, or instead lumps everything into 'Other' if I toggle on that option. I have shared the map publicly so that the expression and setup can be viewed.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm not sure if I'm running into an undocumented limitation or what. I have been scouring forums and have seen many examples that are powering a style with an integer returned by an expression, but I haven't been able to pinpoint why mine is failing. Any ideas?&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="java"&gt;var fieldList = ['Dorian_2019','Sally_2020','Ian_2022','Nicole_2022','Idalia_2023']

var total = 0

for (var f in fieldList) {
    If ($feature[fieldList[f]] == 'YES') {
        total += 1
    }
}

return total&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;This spits out a simple integer. All of the results are between 0 and 4.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="AaronKoelker_0-1699047299262.png" style="width: 999px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/85042i8E29AF205745F1E0/image-size/large?v=v2&amp;amp;px=999" role="button" title="AaronKoelker_0-1699047299262.png" alt="AaronKoelker_0-1699047299262.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Nov 2023 17:15:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-online-questions/map-viewer-styles-with-arcade-expression-working/m-p/1345822#M55664</guid>
      <dc:creator>AaronKoelker</dc:creator>
      <dc:date>2023-11-06T17:15:26Z</dc:date>
    </item>
    <item>
      <title>Re: Map Viewer Sytles with Arcade Expression: Working... but not actually</title>
      <link>https://community.esri.com/t5/arcgis-online-questions/map-viewer-styles-with-arcade-expression-working/m-p/1345939#M55669</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/254600"&gt;@AaronKoelker&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I don't know the exact answer but my suspicion is that the arcade profile for symbology may not support the 'for' loop. I can see you expression does return a number and works perfectly in the pop-ups but struggles to use it when setting symbology. Maybe someone smarter than me can confirm that for us.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I managed to get the symbology to work using a slightly tweaked arcade expression.&lt;BR /&gt;Hope that helps &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;LI-CODE lang="c"&gt;var total = 0
if ($feature.Dorian_2019 == "YES") {
  total = total + 1
}
if ($feature.Ian_2022=="YES") {
  total = total + 1
}
if ($feature.Idalia_2023=="YES") {
  total = total + 1
}
if ($feature.Nicole_2022=="YES") {
  total = total + 1
}
return total&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="gis_KIWI4_0-1699235054713.png" style="width: 999px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/85060i431C0F2DE4579360/image-size/large?v=v2&amp;amp;px=999" role="button" title="gis_KIWI4_0-1699235054713.png" alt="gis_KIWI4_0-1699235054713.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Nov 2023 01:46:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-online-questions/map-viewer-styles-with-arcade-expression-working/m-p/1345939#M55669</guid>
      <dc:creator>gis_KIWI4</dc:creator>
      <dc:date>2023-11-06T01:46:30Z</dc:date>
    </item>
    <item>
      <title>Re: Map Viewer Sytles with Arcade Expression: Working... but not actually</title>
      <link>https://community.esri.com/t5/arcgis-online-questions/map-viewer-styles-with-arcade-expression-working/m-p/1346126#M55686</link>
      <description>&lt;P&gt;Thanks! Your alternative works well. For any ESRI staff that may happen to come across this: if the FOR loop is indeed the issue, it would be nice to have that sort of stuff outlined in the Arcade/Map Viewer documentation, or perhaps a warning visible in the Styles or Arcade editor window when it notices a loop being used. Or if it exists somewhere I haven't found yet, I'd love to get a link.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Appreciate your help!&lt;/P&gt;</description>
      <pubDate>Mon, 06 Nov 2023 16:04:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-online-questions/map-viewer-styles-with-arcade-expression-working/m-p/1346126#M55686</guid>
      <dc:creator>AaronKoelker</dc:creator>
      <dc:date>2023-11-06T16:04:45Z</dc:date>
    </item>
    <item>
      <title>Re: Map Viewer Sytles with Arcade Expression: Working... but not actually</title>
      <link>https://community.esri.com/t5/arcgis-online-questions/map-viewer-styles-with-arcade-expression-working/m-p/1346179#M55687</link>
      <description>&lt;P&gt;It's probably not an issue with the for loop. Usually when you use a variable in retrieving feature attributes, you have to use the &lt;A href="https://developers.arcgis.com/arcade/function-reference/feature_functions/#expects" target="_self"&gt;Expects&lt;/A&gt; function at the top of the script. This ensures that those attributes are getting requested properly.&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;Expect($feature, 'Dorian_2019','Sally_2020','Ian_2022','Nicole_2022','Idalia_2023');
var fieldList = ['Dorian_2019','Sally_2020','Ian_2022','Nicole_2022','Idalia_2023']
var total = 0

for (var f in fieldList) {
    If ($feature[fieldList[f]] == 'YES') {
        total += 1
    }
}

return total&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Nov 2023 16:42:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-online-questions/map-viewer-styles-with-arcade-expression-working/m-p/1346179#M55687</guid>
      <dc:creator>KenBuja</dc:creator>
      <dc:date>2023-11-06T16:42:20Z</dc:date>
    </item>
    <item>
      <title>Re: Map Viewer Sytles with Arcade Expression: Working... but not actually</title>
      <link>https://community.esri.com/t5/arcgis-online-questions/map-viewer-styles-with-arcade-expression-working/m-p/1346195#M55689</link>
      <description>&lt;P&gt;Ah! Interesting. I did not know about the Expects function. Your solution works as well (I did have to add an 's' to 'Expect'). Appreciate the help!&lt;/P&gt;</description>
      <pubDate>Mon, 06 Nov 2023 17:02:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-online-questions/map-viewer-styles-with-arcade-expression-working/m-p/1346195#M55689</guid>
      <dc:creator>AaronKoelker</dc:creator>
      <dc:date>2023-11-06T17:02:21Z</dc:date>
    </item>
    <item>
      <title>Re: Map Viewer Sytles with Arcade Expression: Working... but not actually</title>
      <link>https://community.esri.com/t5/arcgis-online-questions/map-viewer-styles-with-arcade-expression-working/m-p/1346197#M55690</link>
      <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/2839"&gt;@KenBuja&lt;/a&gt;&amp;nbsp; is correct. You should use the Expects function for this case if using a for loop.&lt;/P&gt;&lt;P&gt;The rendering engine for Online (JS API) is optimized for performance. So it only requests the data that's required by the layer's style by default. If you specify a field outside of an Arcade expression, the API knows to request data from that field for rendering. In the case of Arcade, the expression is evaluated for the fields required to make it work. When $feature.FIELDNAME is used, it's easy for the engine to figure out the required fields. That's why the alternative expression works. However, you can use Expects to directly tell the rendering engine which fields it should &lt;EM&gt;expect&lt;/EM&gt; to request from the service. This can be a list of fields as Ken points out, or&amp;nbsp; you can also define template strings like the following:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;Expects("*"); // requests all fields. Don't do this if you have a ton of attributes.

Expects("Year_*") // requests all fields that begin with "Year_". (e.g. "Year_2000", "Year_2010") would be requested, but "Y2004" wouldn't). &lt;/LI-CODE&gt;&lt;P&gt;On a side note, you could take advantage of the number function to condense the initial expression like this:&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;Expects($feature,'*_20*');
var fieldList = ['Dorian_2019','Sally_2020','Ian_2022','Nicole_2022','Idalia_2023'];
var total = 0;

for (var f in fieldList) {
  // adds 1 if "YES"
  // adds 0 if not "YES"
  total += Number($feature[fieldList[f]] == 'YES');
}
return total;&lt;/LI-CODE&gt;&lt;P&gt;Or use the reduce function to avoid a for loop altogether. You still need Expects for this to work...&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;Expects($feature,'*_20*');
var fieldList = ['Dorian_2019','Sally_2020','Ian_2022','Nicole_2022','Idalia_2023'];

function add (total, fieldName){ total + Number($feature[fieldName] == 'YES') }
Reduce(fieldList, add, 0);&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Nov 2023 17:05:26 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-online-questions/map-viewer-styles-with-arcade-expression-working/m-p/1346197#M55690</guid>
      <dc:creator>KristianEkenes</dc:creator>
      <dc:date>2023-11-06T17:05:26Z</dc:date>
    </item>
    <item>
      <title>Re: Map Viewer Sytles with Arcade Expression: Working... but not actually</title>
      <link>https://community.esri.com/t5/arcgis-online-questions/map-viewer-styles-with-arcade-expression-working/m-p/1346219#M55693</link>
      <description>&lt;P&gt;Thank you Kristian, that is all very useful as well! Would you expect a performance benefit using the Reduce function instead of a for loop on larger datasets? I tried both ways on my data, but didn't see a noticeable difference (perhaps became it is relatively small).&lt;/P&gt;</description>
      <pubDate>Mon, 06 Nov 2023 17:30:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-online-questions/map-viewer-styles-with-arcade-expression-working/m-p/1346219#M55693</guid>
      <dc:creator>AaronKoelker</dc:creator>
      <dc:date>2023-11-06T17:30:55Z</dc:date>
    </item>
    <item>
      <title>Re: Map Viewer Sytles with Arcade Expression: Working... but not actually</title>
      <link>https://community.esri.com/t5/arcgis-online-questions/map-viewer-styles-with-arcade-expression-working/m-p/1346307#M55707</link>
      <description>&lt;P&gt;Not necessarily a performance benefit. Just an alternative that is fewer lines. I try to go with the shortest expression. Though I'd say the for loop version is more readable than the reduce approach.&lt;/P&gt;</description>
      <pubDate>Mon, 06 Nov 2023 19:51:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-online-questions/map-viewer-styles-with-arcade-expression-working/m-p/1346307#M55707</guid>
      <dc:creator>KristianEkenes</dc:creator>
      <dc:date>2023-11-06T19:51:13Z</dc:date>
    </item>
    <item>
      <title>Re: Map Viewer Styles with Arcade Expression: Working... but not actually</title>
      <link>https://community.esri.com/t5/arcgis-online-questions/map-viewer-styles-with-arcade-expression-working/m-p/1346310#M55708</link>
      <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/44467"&gt;@KristianEkenes&lt;/a&gt;&amp;nbsp;&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/2839"&gt;@KenBuja&lt;/a&gt;&amp;nbsp;- Thanks for the explanation.&amp;nbsp;&lt;BR /&gt;Learnt something new today - this goes straight into my list of resources &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 06 Nov 2023 19:53:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-online-questions/map-viewer-styles-with-arcade-expression-working/m-p/1346310#M55708</guid>
      <dc:creator>gis_KIWI4</dc:creator>
      <dc:date>2023-11-06T19:53:33Z</dc:date>
    </item>
  </channel>
</rss>

