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:
Year | FID |
2018 | 0 |
2019 | 1 |
2020 | 2 |
2021 | 3 |
2022 | 4 |
2023 | 5 |
Any suggestions would be greatly appreciated.
Thanks everyone.
~Dan
Solved! Go to Solution.
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.
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.