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.
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!
Solved! Go to Solution.
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;
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;
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;
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
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:
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
Hi @Anonymous User ,
Can you share the expression you have now?
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;
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)
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.
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.