Create a FeatureSet from an Array...

465
2
Jump to solution
05-03-2023 01:22 PM
Labels (1)
DJB
by
New Contributor III

I'm trying to create a FeatureSet from an Array.  I've setup a for loop to cycle through the array and add a new row for each record in the array.  The issue that I'm running into is I cannot figure out how to add a new row.  So I am always overwriting the previous record.

My array is  YearList = [2018,2019,2020,2021,2022,2023] and I would like a table like:

YearFID
20180
20191
20202
20213
20224
20235

 

var YearList = [2018,2019,2020,2021,2022,2023]

for (var Yr in YearList){
  var yearDict = {fields: [{alias: "Year",name: "Year",type: "esriFieldTypeInteger",}],spatialReference: '',geometryType: "",features: [{geometry: "",attributes: {Year: YearList[Yr]},}]};
};

return FeatureSet(Text(yearDict));
 
DJB_0-1683145240555.png


Any suggestions would be greatly appreciated.

Thanks everyone.

~Dan

0 Kudos
1 Solution

Accepted Solutions
jcarlson
MVP Esteemed Contributor

You are defining your yearDict object inside the loop, so it's getting recreated every time, losing any previous entries. Define it first outside of the loop, and use your array to populate the features array within it:

var YearList = [2018,2019,2020,2021,2022,2023]

var yearDict = {
  fields: [{alias: "Year", name: "Year", type: "esriFieldTypeInteger"}],
  geometryType: "",
  features: []
}    

for (var Yr in YearList){
  Push(
    yearDict['features'],
    {attributes: {Year: YearList[Yr]}}
  )
};

return FeatureSet(Text(yearDict));

Also, you don't need to specify the geometry or spatial reference if there isn't any.

- Josh Carlson
Kendall County GIS

View solution in original post

0 Kudos
2 Replies
jcarlson
MVP Esteemed Contributor

You are defining your yearDict object inside the loop, so it's getting recreated every time, losing any previous entries. Define it first outside of the loop, and use your array to populate the features array within it:

var YearList = [2018,2019,2020,2021,2022,2023]

var yearDict = {
  fields: [{alias: "Year", name: "Year", type: "esriFieldTypeInteger"}],
  geometryType: "",
  features: []
}    

for (var Yr in YearList){
  Push(
    yearDict['features'],
    {attributes: {Year: YearList[Yr]}}
  )
};

return FeatureSet(Text(yearDict));

Also, you don't need to specify the geometry or spatial reference if there isn't any.

- Josh Carlson
Kendall County GIS
0 Kudos
DJB
by
New Contributor III

@jcarlson,

Thank you for the SUPER quick response.  Worked perfectly!

Thanks again.

~Dan

0 Kudos