Arcade For Loop & Array

4956
13
Jump to solution
08-02-2022 12:43 PM
ChristineSeidel
New Contributor III

Hi - I'm using ArcPro 3.0.0 and I'm new to Arcade.  Using Calculate Field, I trying to write a bit of Arcade code so it loops thru the values of existing field which has a string of concatenated values (i.e.  -99,1,2).  Where an item in this string does not equal -99, I want to append the value to a new array and then ultimately return the Mean of the NewArray.  So, given -99,1,2 the resulting Mean should = 1.5

So, I've tried the following code but it doesn't work.  It just returns Null values:

var NewArray = []
var OrigArray = split($feature.CONCATENATE_Story_Num, ",")
For (var i in OrigArray) {
  If (i!=-99) {
    NewArray+=OrigArray[i];
    }
}
return Mean(NewArray)

Thanks

13 Replies
ChristineSeidel
New Contributor III
Thanks for this solution, Josh. This is exactly what I was looking for. While my original input field is a string of concatenated comma delineated numbers, once it's Split and then recognized as an Array, I didn't have to do any other conversion of string to number.

Thank you!
-Chris

Chris Seidel (pronouns - she, her, hers)
Cartographer/GIS Coordinator
Martha's Vineyard Commission<>
Dukes County Interactive Maps<>
Dukes County GIS Data Hub<>
Mailing: PO Box 1447 Oak Bluffs MA 02557
Physical: 33 New York Ave Oak Bluffs MA
Office: 508-693-3453 x120 (Tuesday thru Friday 8:00AM to 4:00PM)
Saturdays - working from home; please email: seidel@mvcommission.org
jcarlson
MVP Esteemed Contributor

You're welcome! The Arcade Playground and Pro must behave a little differently under the hood, as I couldn't use mathematical functions against the array when I tested it. But I'm glad you got it to work, regardless!

- Josh Carlson
Kendall County GIS
JwHayes
New Contributor III

Hello Everyone,

I've been trying to apply the great examples in this thread to my circumstance but keep coming up short. I want to convert the text values to number from getData function to the array. I've been trying Push and Number functions but I haven't been able to make it work.

I'm using an address point to query underlying layers, like Plats. There can be multiple Plats overlapping an address point. Ultimately, I want find the most recent Plat and append the Filename to #2 below. 

The "Recorded" date field is formatted mm/dd/yyyy.

I included some commented code that I tried. Any help is appreciated. Thanks in advance!!

var getData = FeatureSetByName($datastore, "Plat", ["Filename", "Recorded"])
var PlatDataInt = Intersects(getData, $feature)

var platlist = ""
//var newarray = []

for (var k in PlatDataInt){
    platlist += Right(k.Recorded, 4) + TextFormatting.NewLine
     //Number(platlist[k]){
       //Push(newarray, platlist[k]);
 //}
}

return platlist
//return Max(platlist)
// #1 if (Val == null) return "https://mywebserver/images/survey/NoImage.pdf" 
// #2 "https://mywebserver/images/survey/Plats/" + Text(Filename)

 

 

 

0 Kudos
JwHayes
New Contributor III

I was able to get the most recent year from the intersection plats. However, I got so head-down in this I forgot I need to pull the Filename for the most recent plat year. 

var getData = FeatureSetByName($datastore, "Plat", ["Filename", "Recorded"])
var platint = Intersects(getData, $feature)

var platlist = []

for (var k in platint){
    var rdate = k.Recorded
    var newdate = Right(rdate, 4)
    var numdate = Number(newdate, '####.')
        Push(platlist, numdate)
        
}
var doc = Max(platlist)

return doc

 

0 Kudos