Select to view content in your preferred language

Arcade for loop not iterating properly

381
7
Jump to solution
a week ago
JoeGualdarrama
Emerging Contributor

Hello,

We have a feature set we are trying to iterate through to add text to a popup.  However, when we try to loop through it, it appears to only be accessing one item in the set multiple times. 

 

var related_data = Filter(evtsummary, sql);

var cnt = Count(related_data);
var evtlist = []
Console(cnt)
if (cnt > 0) {
  for (var feat in related_data){
    Console(Text(feat))
    Push(evtlist,feat.name)
  }
  return Concatenate(evtlist,"\n")
  
} else {
    return "-"
}

 

cnt is showing a value consistent with the number of records we expect to see in our testing, but the Console log on line 8 is just the same feature {cnt} times.  Every other step along the way the data is returned as expected.  I cannot find any information on how to view the related_data object in the console to confirm it isn't just the same record multiple times.  Any help would be greatly appreciated.

 

Joe

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JoeGualdarrama
Emerging Contributor

This was a hosted feature layer that was created with a custom data feed.  During creation I was specifying which field to use as the OID, it appears that some interaction along the way did not like this.  We removed that and let it create its own OID and now it seems to be working properly.  Thank you all for the help.

View solution in original post

7 Replies
DavidSolari
MVP Regular Contributor

I hopped in the Arcade playground and used this modified code:

// Fetches features from a public portal item
var evtsummary = FeatureSetByPortalItem(
  Portal("https://www.arcgis.com"),
  // portal item id
  "6200db0b80de4341ae8ee2b62d606e67",
  0, // layer id
  ["*"], // fields to include
  false // include or exclude geometry
);
var sql = "OBJECTID < 10"

var related_data = Filter(evtsummary, sql);

var cnt = Count(related_data);
var evtlist = []
Console(cnt)
if (cnt > 0) {
  for (var feat in related_data){
    Console(Text(feat))
    Push(evtlist,feat.BUILDINGID)
  }
  return Concatenate(evtlist,"\n")
  
} else {
    return "-"
}

and I was unable to replicate your issue. What's your SQL query? If you apply the same query to the table directly in Pro or the map viewer do you get distinct rows back? If that leads nowhere, you might be pulling evtsummary in an odd way that leads to duplicates. If you can't get anywhere you might have to contact your support to see if they can replicate this with your data and Enterprise/Pro versions.

JoeGualdarrama
Emerging Contributor

The feature layer was being created through a custom data feed.  The query was returning the correct info in all the other places we could test the endpoint.  It appears to have been an issue with the OID.  Thanks.

0 Kudos
KenBuja
MVP Esteemed Contributor

That code should work perfectly fine. It works in the Playground with some testing data.

Snag_1985989.png

You should look at one of the attributes in the related_data object to see if it changes (line 8 )

Console(feat.OBJECTID)
JoeGualdarrama
Emerging Contributor

Logging one of the attributes instead of the entire feature was giving similar results.  I had specified a unique field to use as the OID, once I removed that and let the server generate the OID on the fly, it started working correctly.  Thanks for your assistance.

0 Kudos
DavidColey
MVP Regular Contributor

Can we move this to the Arcade Users group?

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

I think leaving it here makes the most sense since the issue wasn't with Arcade code itself but how a published service was behaving with Arcade.

JoeGualdarrama
Emerging Contributor

This was a hosted feature layer that was created with a custom data feed.  During creation I was specifying which field to use as the OID, it appears that some interaction along the way did not like this.  We removed that and let it create its own OID and now it seems to be working properly.  Thank you all for the help.