Yesterday I learned how to create an Array, sort that array and keep the seconf record from it. Today I wanted to try it using a featureSet as it allowed my to sort on a single numeric field. My successful code is below:
// create 50mi buffer on selection
var buff = Buffer($feature,50,'mile')
// get intersecting staff locations
var staff = Intersects(FeatureSetByName($map, "InventoryIntersects"),buff)
// create empty featureset to hold results
var New_FS = {fields:[
{'name':'Development_Name','type': 'esriFieldTypeString'},
{'name':'Project_Address','type': 'esriFieldTypeString'},
{'name':'Project_City','type': 'esriFieldTypeString'},
{'name':'Project_State','type': 'esriFieldTypeString'},
{'name':'Zip_Code','type': 'esriFieldTypeDouble'},
{'name':'Distance','type': 'esriFieldTypeDouble'}],
'geometryType': '',
'features':[]
}
for (var s in staff){
var dist = DistanceGeodetic(s,$feature,'miles')
var distNum = Round(Number(dist),2)
var dName = s.Development_Name
var dAddress = s.Project_Address
var dCity = s.Project_City
var dState = "TX"
var zipcode = s.Zip_Code
var new_f = {'attributes': {'Distance': distNum, 'Zip_Code': zipcode, 'Development_Name': dName, 'Project_Address': dAddress, 'Project_City': dCity, 'Project_State': dState}
}
Push(New_FS.features, new_f)}
var sorted = Top(OrderBy(FeatureSet(Text(New_FS)), 'Distance ASC'),2)
var topTwo = sorted
var filtered = Filter(topTwo, 'Distance > 0')
return filtered
which results in:
Is there a way I could simply convert this to a text string so I could use it in a popup like below?
" The location is at Padre De Vida Apartments - 3900 S Ware Rd, McAllen TX 78503 - 1.14 miles"
Thanks so much.
GRMapper
Solved! Go to Solution.
When you've got your filtered FeatureSet, you can then use the function First to access the first record from the set. Then you can access its attributes directly. Try this at the end of your expression:
// picking up at the end of your expression
var filtered = Filter(topTwo, 'Distance > 0')
var f = First(filtered)
// build output string with a template literal
return `The location is at ${f['Development_Name']} - ${f['Project_Address']}, ${f['Project_City']} ${f['Project_State']} ${f['Zip_Code']} - ${f['Distance']} miles`
Thank you Namenbo. I unfortunately don’t follow. I’m hoping to somehow just convert or parse the fields into a text string to put in a popup. Perhaps I have to dump this into an array. Likely more effort than is needed.
When you've got your filtered FeatureSet, you can then use the function First to access the first record from the set. Then you can access its attributes directly. Try this at the end of your expression:
// picking up at the end of your expression
var filtered = Filter(topTwo, 'Distance > 0')
var f = First(filtered)
// build output string with a template literal
return `The location is at ${f['Development_Name']} - ${f['Project_Address']}, ${f['Project_City']} ${f['Project_State']} ${f['Zip_Code']} - ${f['Distance']} miles`
Josh,
I was just digging into the First function and bam you were there to help!!!! It worked perfectly. Thank you so much.
Greg