Select to view content in your preferred language

Understanding Arcade Code

1405
5
Jump to solution
01-05-2024 10:59 AM
Labels (1)
Ed_
by MVP Regular Contributor
MVP Regular Contributor

Hello and Happy New Year everyone 🙂

So the following map computes the Index under Symbology via Arcade which is cool but I would like to add the Index as a column/attribute/field to the data. The code runs fine but since I am still new to Arcade can you @LisaB as I believe it's your code please explain what the following functions/syntax do? (I have added comments next to the syntax that I am having trouble understanding). Cheers!

 

Code:

 

 

 

var fields = [
  { value: $feature["B17020_calc_pctPovE"], comparison: 13, alias: "Poverty rate" }, // what is value, comparison and it's value and alias doing?
  { value: $feature["B08201_calc_pctNoVehE"], comparison: 9, alias: "Households without access to a vehicle" },
  { value: $feature["B28002_calc_pctNoIntE"], comparison: 14, alias: "Households without access to internet" },
  { value: $feature["B27010_calc_pctNoInsE"], comparison: 9, alias: "Households without health insurance" },
  { value: $feature["B01001_calc_pctDependE"], comparison: 38, alias: "Dependent population (Ages <18 or 65+)" }
];

function calcFactor(fieldsArray){
    var factorCount = 0 // Why add factorCount?
    var factorTotal = 0 // // Why add factorTotal?
    for(var x in fieldsArray){
        var val = fieldsArray[x].value // What's the syntax doing here?
        var comp = fieldsArray[x].comparison // What's the syntax doing here?
        var indexValue = (val/comp)*100 // Why divide val by comp?
        factorTotal += indexValue // What's the syntax doing here?
        factorCount += 1 // What's the syntax doing here?
    }
    var overallIndex = factorTotal/factorCount  
    var indexText = when(overallIndex < 60, "Lowest Stress", overallIndex >= 60 && overallIndex < 90, "Low Stress", overallIndex >= 90 && overallIndex < 110, "Average Stress", overallIndex >= 110 && overallIndex < 140, "High Stress", overallIndex >= 140, "Highest Stress", "N/A")
    return indexText
}

 

 

 

Question | Analyze | Visualize
Tags (1)
0 Kudos
1 Solution

Accepted Solutions
AlfredBaldenweck
MVP Regular Contributor

The factorCount and Total are used to calculate the overall index.

line 13 val--> looks in the fieldsArray and grabs the value of the "value" key

line 14 comp --> same thing but the comparison key

line 15  get the percentage

line 16 adds the indexValue for this item to the factor total

line 17 just counts how many items you've gone through.

View solution in original post

5 Replies
Ed_
by MVP Regular Contributor
MVP Regular Contributor

Hi @jcarlson hope all is well, when you have the time, can you please help on this? Thank you!

Question | Analyze | Visualize
0 Kudos
AlfredBaldenweck
MVP Regular Contributor

The factorCount and Total are used to calculate the overall index.

line 13 val--> looks in the fieldsArray and grabs the value of the "value" key

line 14 comp --> same thing but the comparison key

line 15  get the percentage

line 16 adds the indexValue for this item to the factor total

line 17 just counts how many items you've gone through.

Ed_
by MVP Regular Contributor
MVP Regular Contributor

Thank you so much for your guidance on this 🙂. I have some follow up questionz please, what are lines 1 to 7 doing? Like I know a variable called fields (also is fields a list or a data frame?) is being defined but like we can use {} to define multiple values (value, comparison and alias in this) within a variable? Also value is grabbing values from those respective columns right? And comparison values are hard coded right like 13, 9 and so on? And lastly what's the purpose of defining alias?

Question | Analyze | Visualize
0 Kudos
AlfredBaldenweck
MVP Regular Contributor

Lines 1-7 are a list of dictionaries broken up into different lines for readability. A dictionary has one or more keys, each with a value associated with it. so in this case, the keys are "value", "comparison", and "alias". Each dictionary is structured as {key:value, key2:value2}. Compare to a list, which is structured with [item1, item2, item 3].

So, the function's code takes this list of dictionaries, then for each dictionary, looks up the value for the given key (lines13 and 14).* 

I'm honestly not sure why there's an alias key in there except maybe for readability; it doesn't seem to get used.

Also, the fields variable is fed into the calcFactor function, inside of which it's called fieldsArray.

 

 

*Sidenote: Unlike in Python, if you're doing a for loop on a list, the value of the iteration is the index of the item in the list. So you have to look it up by feeding it the index. See below:

Python:

list = ["apple", "pear", "banana"]

for l in list:
    print(l)

# "apple"
# "pear"
# "banana"

 

Arcade:

// The way you'd expect if coming from Python
var list = ["apple", "pear", "banana"]

for var l in list {
    console(l)
}
// Yields:
// 0
// 1
// 2


// The actual way to get what you want
for var l in list {
    console(list[l])
}
// Yields:
// "apple"
// "pear"
// "banana"

 

Ed_
by MVP Regular Contributor
MVP Regular Contributor

Thank you so much for the detailed answer 🙂 and also for the comparison explanation between Arcade and Python.

Question | Analyze | Visualize
0 Kudos