Almost all languages, if not all, have a construct for handling these types of situations better than large else-if statements. For Arcade, it is the When statement:
When
When(expression1, result1, [expression2, result2, ..., expressionN, resultN]?, defaultValue) -> Any
Evaluates a series of conditional expressions until one evaluates to true.
Since Arcade is a young-ish language, it continues to evolve with new releases happening every few months on average. I point that out because it is really important to state what version of ArcGIS and Arcade software is being used since code written with newer functions won't run on older versions.
// Using original Arcade functions var zone_codes = [ "C-3", "C-1", "C-2", "R-3", "0-3", "C-2", "CB-1", "I-1" ]; var zone_groups = []; for(var i in zone_codes) { var z = Split(zone_codes[i], "-", -1)[0]; zone_groups[i] = When( IndexOf(["C", "CB"], z) > -1, "Commercial", IndexOf(["I"], z) > -1, "Industrial", IndexOf(["R"], z) > -1, "Residential", "Miscellaneous" ); z = ""; }; return zone_groups; // Using newer Arcade (1.12+) functions var zone_codes = [ "C-3", "C-1", "C-2", "R-3", "0-3", "C-2", "CB-1", "I-1" ]; var zone_groups = []; for(var i in zone_codes) { var z = Split(zone_codes[i], "-", -1)[0]; Push(zone_groups, When( Includes(["C", "CB"], z), "Commercial", Includes(["I"], z), "Industrial", Includes(["R"], z), "Residential", "Miscellaneous" )); z = ""; }; return zone_groups;
Returns ["Commercial","Commercial","Commercial","Residential","Miscellaneous","Commercial","Commercial","Industrial"]