Looking to export contents of Feature Table to CSV

1544
3
10-24-2017 08:10 AM
PamTurner
New Contributor III

Hi All - I'm looking to use the sample referenced below to export the contents of a Feature Table to a CSV file in a custom JavaScript application. 

JS Bin - Collaborative JavaScript Debugging 

It appears that starting at version 3.18 of the JS API, the array generated using FeatureTable.dataStore.data comes back as a JSON object versus an array of attributes and the sample no longer works.

I can get at all the attributes using the following, then iterating over each record returned, but it returns all attributes, not just the ones visible in the table and it seems like a overly complicated way to accomplish what I'm trying to do.

Object.keys(featTable.dataStore.data[Object.keys(featTable.dataStore.data)[0]].attributes)

Has anyone tried to do this in the newer versions of the API? Or know of an easier way to do this?

A similar question was asked about a year ago but there were no responses.

Feature Table Export 3.18 to CSV 


Thank you in advance.

Tags (2)
0 Kudos
3 Replies
ThomasSolow
Occasional Contributor III

Just to be clear, which version of the API do you want to use for this?  3.18?

0 Kudos
PamTurner
New Contributor III

I apologize for not being clear. I am looking to use the most current version, 3.22. I only referenced 3.18 because it appeared to be the version where that sample no longer worked.


Thanks!

0 Kudos
ThomasSolow
Occasional Contributor III

There's two issues that I notice, the first is that the attributes aren't set up like the code expects, like you mentioned.

The second is that the array has ~10,000 undefined elements based on the objectids.  I don't really know what to make of this, it seems unintentional to me.

Anyway here's a simple fix: JS Bin - Collaborative JavaScript Debugging 

data = Array.from(new Set(value.data)) // this makes sure there are no dupes in array
       .filter(d => d) // filter out nulls/undefined
       .map(d => d.attributes); // map to attributes

You could just filter the array like this: data = data.filter(d => d) before mapping instead of using a Set if your browser doesn't support that.  Set should be faster though.