How Do I step through (loop over) a feature/graphic's attributes in JSAPI?

1112
2
Jump to solution
02-26-2020 09:04 AM
Arne_Gelfert
Regular Contributor

Working with JSAPI v.3.31 on WAB DE widget stuff. So I have a query that returns a feature set, in fact it's a single feature, so that:..

var resultFeatures = featureSet.features;
var graphic = resultFeatures[0];

. Then I provide some styling and add it to the map

var symbol = new SimpleFillSymbol();
symbol.setColor(new Color([255,0,51,0.5]));
graphic.setSymbol(symbol); 
this.map.graphics.add(graphic);‍‍‍‍

I can access the attributes of the feature using:

var attributes = graphic.attributes‍

But I can't figure out how to step through them, to selectively add them to an InfoTemplate or otherwise process the attribute info. All of the below has failed because attributes is type Object..

attributes.forEach( function()...)
// or
dojo.forEach(attribute,function()...)
// or
array.forEach(attribute,function()...)‍‍‍‍‍‍‍‍‍

What's the solution here? I don't want to have to access each attribute directly:

grahic.attributes['myattribute']

THANKS!

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Esteemed Contributor

Arne,

   The issue you are having is that you are trying to treat the graphic.attributes as an array when it is an Object

https://developers.arcgis.com/javascript/3/jsapi/graphic-amd.html#attributes 

To loop through the object you can use Object.entries

Object.entries() - JavaScript | MDN 

View solution in original post

2 Replies
RobertScheitlin__GISP
MVP Esteemed Contributor

Arne,

   The issue you are having is that you are trying to treat the graphic.attributes as an array when it is an Object

https://developers.arcgis.com/javascript/3/jsapi/graphic-amd.html#attributes 

To loop through the object you can use Object.entries

Object.entries() - JavaScript | MDN 

Arne_Gelfert
Regular Contributor

Sweet, worked like a charm! Plus, the syntax isn't as gross as some I've seen. Haha. Reminds of stepping through k,v n Python dict items. - I'll have another questions for you in a separate thread this afternoon. 

0 Kudos