Select to view content in your preferred language

Error in New Expression using Arcade - (var feature in fs) - feature already defined

622
4
Jump to solution
02-25-2024 11:12 PM
mfreeman
Occasional Contributor

Hello,

I don't know anything in Arcade but I am following someone else code to split the answer of a multiselect question and generate a chart that counts each answer separatly.

The code is as follow

// Reference layer using the FeatureSetByPortalItem() method.
var p = Portal("https://arcgis.com");
var fs = FeatureSetByPortalItem(
    p,
    '2ee459d7a5914544bb18ea293dd3be41',
    0,
    ['energy_sel_one'],
    false
);

// Create empty array for features and feat object
var features = [];
var feat;

// Split comma separated hazard types and store in dictionary.  
for (var feature in fs) {
    var split_array  =  Split(feature["energy_sel_one"], ',')
    var count_arr = Count(split_array)
    for(var i = 0; i < count_arr; i++ ){
        feat = {
            'attributes': {
                'split_choices': Trim(split_array[i])
            }
        }
        Push(features, feat);
}}

// Empty dictionary to capture each hazard reported as separate rows.
var choicesDict = {
    'fields': [
        { 'name': 'split_choices', 'type': 'esriFieldTypeString'}],
    'geometryType': '',
    'features': features
};

// Convert dictionary to featureSet.
var fs_dict = FeatureSet(choicesDict);

// Return featureset after grouping by hazard types.
return GroupBy(fs_dict, ['split_choices'],
       [{ name: 'split_count', expression: 'split_choices', statistic: 'COUNT' }]);  
 
However when I run the expression, it returns the following error
mfreeman_0-1708931418632.png

 

Could anyone help me in fixing it knowing that I don't know anything about arcade.

Thank you

0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

The code does work substituting my own portal item. Where is it breaking for you? Are you checking the output at various places in the code with Console to see if it's returning what you're expecting?

var p = Portal("https://noaa.maps.arcgis.com/");
var fs = FeatureSetByPortalItem(
    p,
    '1624f1a86dca4af086cc13141a1b0e2b',
    1,
    ['Authors'],
    false
);

// Create empty array for features and feat object
var features = [];
var feat;
// Split comma separated hazard types and store in dictionary.  
for (var feature in fs) {
    var split_array  =  Split(feature["Authors"], ',')
    var count_arr = Count(split_array)
    console(count_arr)
    for(var i = 0; i < count_arr; i++ ){
        feat = {
            'attributes': {
                'split_choices': Trim(split_array[i])
            }
        }
        Push(features, feat);
}}


// Empty dictionary to capture each hazard reported as separate rows.
var choicesDict = {
    'fields': [
        { 'name': 'split_choices', 'type': 'esriFieldTypeString'}],
    'geometryType': '',
    'features': features
};

// Convert dictionary to featureSet.
var fs_dict = FeatureSet(choicesDict);
//return fs_dict
// Return featureset after grouping by hazard types.
return GroupBy(fs_dict, ['split_choices'],
       [{ name: 'split_count', expression: 'split_choices', statistic: 'COUNT' }]);  

2024-02-27_9-21-44.png

View solution in original post

0 Kudos
4 Replies
KenBuja
MVP Esteemed Contributor

That's not an error, but a warning. You shouldn't use reserved words as variable names. It will still work, but it might cause problems.

0 Kudos
mfreeman
Occasional Contributor

Thank you for your response.

Unfortunately it then don't allow me to progress further and select the expression to build my chart

0 Kudos
KenBuja
MVP Esteemed Contributor

The code does work substituting my own portal item. Where is it breaking for you? Are you checking the output at various places in the code with Console to see if it's returning what you're expecting?

var p = Portal("https://noaa.maps.arcgis.com/");
var fs = FeatureSetByPortalItem(
    p,
    '1624f1a86dca4af086cc13141a1b0e2b',
    1,
    ['Authors'],
    false
);

// Create empty array for features and feat object
var features = [];
var feat;
// Split comma separated hazard types and store in dictionary.  
for (var feature in fs) {
    var split_array  =  Split(feature["Authors"], ',')
    var count_arr = Count(split_array)
    console(count_arr)
    for(var i = 0; i < count_arr; i++ ){
        feat = {
            'attributes': {
                'split_choices': Trim(split_array[i])
            }
        }
        Push(features, feat);
}}


// Empty dictionary to capture each hazard reported as separate rows.
var choicesDict = {
    'fields': [
        { 'name': 'split_choices', 'type': 'esriFieldTypeString'}],
    'geometryType': '',
    'features': features
};

// Convert dictionary to featureSet.
var fs_dict = FeatureSet(choicesDict);
//return fs_dict
// Return featureset after grouping by hazard types.
return GroupBy(fs_dict, ['split_choices'],
       [{ name: 'split_count', expression: 'split_choices', statistic: 'COUNT' }]);  

2024-02-27_9-21-44.png

0 Kudos
mfreeman
Occasional Contributor

Thank you very much - I have figured my error - I used to ID of the form not the result layers and it works

Another questions - my overall dashboard has date and category selectors in the heading section. Is there a way to include them in the expression so that the selectors will work on it? 

Thank you very much

0 Kudos