How do I access the attribute values of a feature when iterating through the featureSet returned by a queryTask?

2263
8
Jump to solution
02-12-2016 10:20 AM
KenPierce
New Contributor III

After returning my featureSet from a query, I want to be able to pass an array to d3 to do some visualizations and analyses. How do I either convert the returned featureSet to an array or get the attributes returned from a feature? The only thing I've managed to do is get a count of features.

array.forEach(featureSet.features, function(feature) {

  featureCount += 1;

  console.log("feature attributes are" + Would love to display a value from an attribute here);

  });

How do I access field values on the feature?

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Ken,

What does your query code look like?

What outFields are you specifying in the query parameters?

View solution in original post

8 Replies
RobertScheitlin__GISP
MVP Emeritus

Ken in your feature loop you just use feature.attributes.yourAttributeName

0 Kudos
KenBuja
MVP Esteemed Contributor

The feature in your function has attributes you can examine. If you know what those attributes are named, then you can specify them. For example, if one of the attributes is called "Class", you can use

array.forEach(featureSet.features, function(feature) {
  featureCount += 1;
  console.log("Class: " + feature.attributes.Class );
});
0 Kudos
KenPierce
New Contributor III

I've been trying that exact syntax for hours and all I get in the console window is undefined. I have an info template for the same unfiltered feature service which works fine so I'm assuming I have the feature names correct. I get a different feature count depending on my query parameter so I'm pretty positive its returning the right stuff. So if there are 62 features returned the console window has a 62 in a little circle with the message "feature attributes are undefined" which I believe means here are 62 rows with the same output.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Ken,

What does your query code look like?

What outFields are you specifying in the query parameters?

KenPierce
New Contributor III

Bingo! I was assuming (stupidly) the simple query returned all fields. That would obviously waste bandwidth and slow down response times so why would that be the default? I had no outFields. Thank you thank you!!

Ken

0 Kudos
KenPierce
New Contributor III

Can you amend your answer to ask about the outFields? I’m happy to pick that as correct but I want to make sure it notes the actual problem for other folks who may come across it.

Ken

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Ken,

   I did mention outFields of the Query parameters in my last reply, so you can mark that one as the correct answer.

0 Kudos
KenPierce
New Contributor III

Hadn't noticed these emails were all being captured. I'm a geonet noob. Thanks!

0 Kudos