Multiple When or if/else statements?

628
2
Jump to solution
07-03-2019 12:35 PM
AlbertGarcia4
New Contributor II

Hello, I'm still a bit fresh at Arcade expressions, I'm hoping someone can help. I'm trying to write an expression for a pop-up that will list various lines of text that I add based on table attributes. For example, the attributes in about 10 fields are either "y" or "n". I would like a sentence, "blah blah blah" to be included in the pop-up if it is a "y", nothing if it's an "n". This I can do with a simple "When" statement:

"When($feature["HiF1"]== "y",'Strong and Engaged Leadership','')

The problem is there are 9 other attributes with accompanying sentences like the example. Is there a way to add multiple "When" statements so that if there are 1, 2, 5, or even all 10 "y" attributes they will each be listed? I've tried if else and IIf, I'm only able to get one to pop-up at a time.

Thank you in advance for any suggestion,

Best Regards,

Albert

0 Kudos
1 Solution

Accepted Solutions
LanceCole
MVP Regular Contributor

Hi Albert, 

If/Else and When statements only return the first true condition.  To do this you will need create a variable to hold your return string or list of values to return, check each condition and then return the string.  Something along the lines of the following: (Code is not tested and meant as a sample only)

var i = 0;
var ret = [];

function add(f, s) {
    if (f == 'y') {
        ret[i++] = s;
    }
}

add($feature["HiF1"], 'Strong and Engaged Leadership');
add($feature["HiF2"], 'The Text for this Feature');
add($feature["HiF3"], 'The Next Feature to Check');
add($feature["HiF4"], 'Etc.');

return Concatenate(ret, ", ")‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

I believe you could also use a dictionary containing the feature name and text to clean this up even more:

var i = 0;
var ret = [];

function add(f, s) {
    if (f == 'y') {
        ret[i++] = s;
    }
}

var d = dictionary(
    "HiF1", 'Strong and Engaged Leadership',
    "HiF2", 'The Text for this Feature',
    "HiF3", 'The Next Feature to Check',
    "HiF4", 'Etc.');
    
for (var x in d){
    add($feature[x], d[x]);
}

return Concatenate(ret, ", ")‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

You can change the comma (", ") in the Concatenate line to what ever you would like to join the strings together. 

View solution in original post

2 Replies
LanceCole
MVP Regular Contributor

Hi Albert, 

If/Else and When statements only return the first true condition.  To do this you will need create a variable to hold your return string or list of values to return, check each condition and then return the string.  Something along the lines of the following: (Code is not tested and meant as a sample only)

var i = 0;
var ret = [];

function add(f, s) {
    if (f == 'y') {
        ret[i++] = s;
    }
}

add($feature["HiF1"], 'Strong and Engaged Leadership');
add($feature["HiF2"], 'The Text for this Feature');
add($feature["HiF3"], 'The Next Feature to Check');
add($feature["HiF4"], 'Etc.');

return Concatenate(ret, ", ")‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

I believe you could also use a dictionary containing the feature name and text to clean this up even more:

var i = 0;
var ret = [];

function add(f, s) {
    if (f == 'y') {
        ret[i++] = s;
    }
}

var d = dictionary(
    "HiF1", 'Strong and Engaged Leadership',
    "HiF2", 'The Text for this Feature',
    "HiF3", 'The Next Feature to Check',
    "HiF4", 'Etc.');
    
for (var x in d){
    add($feature[x], d[x]);
}

return Concatenate(ret, ", ")‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

You can change the comma (", ") in the Concatenate line to what ever you would like to join the strings together. 

AlbertGarcia4
New Contributor II

Hi, Lance, many thanks for both the solution and the explanation - very helpful indeed. I'll be tooling around with the code now, this is exactly what I was looking for.

Thanks again,

Cheers!

Albert

0 Kudos