Select to view content in your preferred language

Creating a new Feature from a list of Fields that contain YES or NO entries with the Expression Editor for Pop-ups

2664
9
Jump to solution
07-06-2021 12:09 PM
by Anonymous User
Not applicable

Hi all,

I have a web map on ArcGIS Online that pulls it's data from a REST service. The REST service shows active farmers markets for my state and it lists what months each market is open in an extremely cumbersome fashion. The attached screen shot shows that each market has a YES or NO value listed 12 different times for a single market inside it's pop-up window. I want to consolidate all of these YES and NO values into a new feature that simply states what months the market is open. 

JamesErvin_0-1625597914266.png 

This is the pop-up for this market as seen through a dashboard, which will be the final application of this map. 

I just want the pop-up to state something along the lines of "Open in: Jun, July, August, September" for each market you click.

It should be noted I don't have access through my organization to make changes to things on it's REST service(s). Hence the need for a workaround. 

I might be using the word "feature" wrong as a solution to my problem so feel free to correct me on that as well. My idea for a workaround was to use the feature or featureset functions in the expression editor to create a new feature that references the existing 12 columns of monthly data for all ~360 markets. I have been trying to learn how to write an Arcade expression that will do this for the past couple of days but all of the different functions are making my head spin and I have never tried to code anything in any language before this. I am still stuck on the stage of how syntax works, haha! 

.

.

.

@XanderBakker Hope you don't mind I pinged you! I saw you offered a seemingly similar solution to the problem linked here. I was hoping maybe you would be the right person to call on for assistance! 

0 Kudos
2 Solutions

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

Hi @Anonymous User ,

I think you could use something like this: 

// create a disctionary with field names and month names (change the field names!)
var dct = {"OpenJan": "January", "OpenFeb": "February",
           "OpenMar": "March", "OpenApr": "April", 
		   "OpenMay": "May", "OpenJun": "June",
		   "OpenJul": "July", "OpenAug": "August",
		   "OpenSep": "September", "OpenOct": "October",
		   "OpenNov": "November", "OpenDec": "December"};

// start with an empty result string
var result = "";

// loop through field names in dct
for (var fldname in dct) {
	// read the value of the field
	var fldval = $feature[fldname];
	// validate if the value is YES
	if (fldval == "YES") {
		// add the month to the result
		if (result == "") {
			// first month
			result = "Open in: " + dct[fldname];
		} else {
			// any next month
			result += ", " + dct[fldname];	
		}
	}
}

return result;	‍
‍

View solution in original post

XanderBakker
Esri Esteemed Contributor

Hi @Anonymous User ,

 

A simple fix could be this:

// create a disctionary with field names and month names (change the field names!)
var dct = {"OpenJan": "January", "OpenFeb": "February",
           "OpenMar": "March", "OpenApr": "April", 
		   "OpenMay": "May", "OpenJun": "June",
		   "OpenJul": "July", "OpenAug": "August",
		   "OpenSep": "September", "OpenOct": "October",
		   "OpenNov": "November", "OpenDec": "December"};

// simple fix, use a list of field names with the right order
var lst = ["OpenJan", "OpenFeb", "OpenMar", "OpenApr", 
		   "OpenMay", "OpenJun", "OpenJul", "OpenAug",
		   "OpenSep", "OpenOct", "OpenNov", "OpenDec"];

// start with an empty result string
var result = "OK";

// start with an empty result string
var result = "";

// loop through the list of field names
for (var i in lst) {
    var fldname = lst[i];
	// read the value of the field
	var fldval = $feature[fldname];
	// validate if the value is YES
	if (fldval == "YES") {
		// add the month to the result
		if (result == "") {
			// first month
			result = dct[fldname];
		} else {
			// any next month
			result += ", " + dct[fldname];	
		}
	}
}

return result;	

View solution in original post

0 Kudos
9 Replies
XanderBakker
Esri Esteemed Contributor

Hi @Anonymous User ,

I think you could use something like this: 

// create a disctionary with field names and month names (change the field names!)
var dct = {"OpenJan": "January", "OpenFeb": "February",
           "OpenMar": "March", "OpenApr": "April", 
		   "OpenMay": "May", "OpenJun": "June",
		   "OpenJul": "July", "OpenAug": "August",
		   "OpenSep": "September", "OpenOct": "October",
		   "OpenNov": "November", "OpenDec": "December"};

// start with an empty result string
var result = "";

// loop through field names in dct
for (var fldname in dct) {
	// read the value of the field
	var fldval = $feature[fldname];
	// validate if the value is YES
	if (fldval == "YES") {
		// add the month to the result
		if (result == "") {
			// first month
			result = "Open in: " + dct[fldname];
		} else {
			// any next month
			result += ", " + dct[fldname];	
		}
	}
}

return result;	‍
‍
by Anonymous User
Not applicable

You're a wizard! It worked perfectly besides listing a couple months in the wrong order, but I can manage fixing that myself I think. 

One more piece of help please! It lists the months in alphabetical order instead of by the order of months in a year. I try and type some solutions to this into your code but I can't seem to do anything other than break it. What to do? @XanderBakker 

JamesErvin_1-1625673383276.png

 

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi @Anonymous User ,

Glad to hear that it helped. If you run into problems with making the required changes just let me know.

About your questions:

  • For example, why use this method instead of creating an entire new feature?
    • I wonder how you would create a new feature if you are not the owner of the data. You could with a notebook access the data, transform it the way you need it, and write a local feature layer and use that. This is more complex to implement.
  • What was the purpose of starting the logic part of the expression with "an empty results string?"
    • Any variable you want to assign a value needs to be initialized. This is done using "var NameOfVariable = InitialValue". Perhaps this could be omitted and line 21 could start with "var ". However, sometimes this could create an error when compiling the code, so I prefer to initialize the variable before the loop.
  • Finally, what exactly do the "{" and "}" symbols do in this expression? 
by Anonymous User
Not applicable

Thanks for your explanations. I will be sure to explore the resources you linked to asap.

One more piece of help please! It lists the months in alphabetical order instead of by the order of months in a year. What to do? @XanderBakker 

JamesErvin_1-1625673383276.png

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi @Anonymous User ,

Can you share the expression you have now?

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi @Anonymous User ,

 

A simple fix could be this:

// create a disctionary with field names and month names (change the field names!)
var dct = {"OpenJan": "January", "OpenFeb": "February",
           "OpenMar": "March", "OpenApr": "April", 
		   "OpenMay": "May", "OpenJun": "June",
		   "OpenJul": "July", "OpenAug": "August",
		   "OpenSep": "September", "OpenOct": "October",
		   "OpenNov": "November", "OpenDec": "December"};

// simple fix, use a list of field names with the right order
var lst = ["OpenJan", "OpenFeb", "OpenMar", "OpenApr", 
		   "OpenMay", "OpenJun", "OpenJul", "OpenAug",
		   "OpenSep", "OpenOct", "OpenNov", "OpenDec"];

// start with an empty result string
var result = "OK";

// start with an empty result string
var result = "";

// loop through the list of field names
for (var i in lst) {
    var fldname = lst[i];
	// read the value of the field
	var fldval = $feature[fldname];
	// validate if the value is YES
	if (fldval == "YES") {
		// add the month to the result
		if (result == "") {
			// first month
			result = dct[fldname];
		} else {
			// any next month
			result += ", " + dct[fldname];	
		}
	}
}

return result;	
0 Kudos
by Anonymous User
Not applicable

@XanderBakker,

As of now, it's unchanged from your original expression you replied with. I was just poking around trying to find a solution by trying different data functions but to no avail. I apologize for any confusion; this is my first time ever asking for coding help of any type so perhaps my ability to correctly frame the problem is somewhat handicapped. 

To clarify, your code worked wonders in displaying the months in a concise manner as I wanted. The only problem is the script defaulted to sorting the months by alphabetical order and I would like to have them displayed in the ordinary order for months. (i.e. January, February, March April, December instead of April, December, February, January, March)

0 Kudos
XanderBakker
Esri Esteemed Contributor

Hi @Anonymous User ,

I just posted a workaround and I also noticed that the order of the dictionary is alphabetically and not the order as one defines it. 

by Anonymous User
Not applicable

@XanderBakker 

Indeed you did! I guess I should have refreshed my page sooner to see your response, whoops. Your simple workaround worked perfectly with no issues; the months display in the correct order. Thanks again for the help! I look forward to going through the resources you linked to.