ARCADE expression like SQL "IN"

2516
5
Jump to solution
03-10-2021 09:21 AM
KariBuckvold
Occasional Contributor

These seems elementary, but I'm not finding it.  I am building an attribute rule using Arcade and I would like to select several different values to meet a criteria.  In SQL, I could do:

IF(fieldname IN ("value1","value2","value3"))

But in ARCADE, I can only seem to do:

IF(fieldname == "value1" || fieldname == "value2" || fieldname == "value3" )

Is there a way to simplify this expression like using the "IN" as in SQL?

3 Solutions

Accepted Solutions
DuncanHornby
MVP Notable Contributor

You could try something like this?

var inputArray = ["xyz","abx","bob"];
var bool = Includes(inputArray, "bob");
return bool;

View solution in original post

JoshuaBixby
MVP Esteemed Contributor

Although Includes will be the path moving forward, it is new in Arcade.  If you are working with < 1.12 Arcade, the pattern is to use IndexOf.

View solution in original post

jcarlson
MVP Esteemed Contributor

That's a very good point! Using IndexOf doesn't return a boolean, so to simply check if it's "in" the list, it would look like this:

var some_list = ['value1', 'value2', 'value3', ... 'valueN']

if(IndexOf(some_list, $feature.field_name) != -1){
    // It's in!
} else {
    // It's not!
}

 

- Josh Carlson
Kendall County GIS

View solution in original post

5 Replies
DuncanHornby
MVP Notable Contributor

You could try something like this?

var inputArray = ["xyz","abx","bob"];
var bool = Includes(inputArray, "bob");
return bool;
jcarlson
MVP Esteemed Contributor

You can accomplish this with Includes. Pass an array of values, and if your feature's field is included in the array, it will evaluate as 'true'.

var some_list = ['value1', 'value2', 'value3', ... 'valueN']

if(Includes(some_list, $feature.field_name)){
    // It's in!
} else {
    // It's not!
}

 

- Josh Carlson
Kendall County GIS
JoshuaBixby
MVP Esteemed Contributor

Although Includes will be the path moving forward, it is new in Arcade.  If you are working with < 1.12 Arcade, the pattern is to use IndexOf.

jcarlson
MVP Esteemed Contributor

That's a very good point! Using IndexOf doesn't return a boolean, so to simply check if it's "in" the list, it would look like this:

var some_list = ['value1', 'value2', 'value3', ... 'valueN']

if(IndexOf(some_list, $feature.field_name) != -1){
    // It's in!
} else {
    // It's not!
}

 

- Josh Carlson
Kendall County GIS
KariBuckvold
Occasional Contributor

Thanks @jcarlson @JoshuaBixby @DuncanHornby , I must have an earlier version of ARCADE since only IndexOf worked...and like a charm!  Sure wish there was a 'google translate' between programming languages, that should have been much easier to figure out than it was!