Select to view content in your preferred language

# Use Arcade to Calculate Field Based on Options from Another Field

1092
7
Jump to solution
09-01-2023 09:15 AM
Labels (3)
MVP Regular Contributor

Hello,

I've got an Arcade question. I have a feature set of points with an attribute "Type" that details the feature type. I have another attribute that is a numeric ID that corresponds to the text attribute Type. Example: Hydrant =1, Curb Stop = 2 etc. What's the best way to write an attribute rule in Arcade that will populate the numeric ID based on the value from the text type? My knee jerk is to do a super long If/Else exchange to the effect of:

``````var waterType = \$feature.Type
if (waterType='Hydrant'){
return 1;
}
if(waterType='Curb Stop'){
return 2;
}
etc``````

However, there are like, 60 possible types which strikes me as a bit onerous to put into an if statement like that. Is there a better way?

Tags (4)
2 Solutions

Accepted Solutions
by
MVP Esteemed Contributor

Here are two possible ways of doing it. It is a little onerous to build it for your 60 types either way, but a little easier than using if statements.

Using a Dictionary

``````var dict = {
'Hydrant': 1,
'Curb Stop': 2
};
var waterType = \$feature.Type
return dict[waterType];``````

Using a When function

``````var waterType = \$feature.Type;
when (waterType == 'Hydrant', 1,
waterType == 'Curb Stop', 2,
-9999 //default value for any other type
)``````
MVP Regular Contributor

I did an abbreviated dictionary/where as a test:

Not working

``````var dict = {
'Gate Valve': 87,
'Curb Stop': 12
};
var waterType = \$feature.Feat_Code
return dict[waterType];``````

Working:

``````var waterType = \$feature.Feat_Code;
when (waterType == 'Gate Valve', 87,
waterType == 'Curb Stop', 12,
-9999 //default value for any other type
)``````

7 Replies
by
MVP Esteemed Contributor

Here are two possible ways of doing it. It is a little onerous to build it for your 60 types either way, but a little easier than using if statements.

Using a Dictionary

``````var dict = {
'Hydrant': 1,
'Curb Stop': 2
};
var waterType = \$feature.Type
return dict[waterType];``````

Using a When function

``````var waterType = \$feature.Type;
when (waterType == 'Hydrant', 1,
waterType == 'Curb Stop', 2,
-9999 //default value for any other type
)``````
MVP Regular Contributor

Aha, yes without knowing it was thinking of a dictionary, which is a data type I'm pretty unfamiliar/uncomfortable with, so I want to try to use them more. However, I get an Arcade error that basically says the expression calculator couldn't find the attribute Type. Any thoughts on that?

Additionally, the second option, the When function, works perfectly!

by
MVP Esteemed Contributor

I wasn't running into any errors on the tests I ran. What did you code look like?

MVP Regular Contributor

I did an abbreviated dictionary/where as a test:

Not working

``````var dict = {
'Gate Valve': 87,
'Curb Stop': 12
};
var waterType = \$feature.Feat_Code
return dict[waterType];``````

Working:

``````var waterType = \$feature.Feat_Code;
when (waterType == 'Gate Valve', 87,
waterType == 'Curb Stop', 12,
-9999 //default value for any other type
)``````

by
MVP Esteemed Contributor

Does Feat_Code have a Domain? If so, you'd have to use this code

``````var dict = {
'Gate Valve': 87,
'Curb Stop': 12
};
return dict[DomainName(\$feature, 'Feat_Code')];``````

MVP Regular Contributor

It does have a domain, and I had tried to use it, but I had entered it as part of the variable, like this:

``````var dict = {
'Gate Valve': 87,
'Curb Stop': 12
};
var waterType = DomainName(\$feature,Feat_Code)
return dict[waterType];``````

That's clarifying, thank you!

by
MVP Esteemed Contributor

Your way would have worked, but you were missing the quotes around 'Feat_Code'