I am working on a web app where I am making calls to a database api and it returns a JSON Object. I've managed to do so successfully as well as any manipulations that need be done.

Is there a way to add new fields to a FeatureLayer and the subsequently populate with the relevant data?

I do not necessarily want to edit the feature layer, just attach data on the fly so that I can then use it for analysis such as heat maps and what not. Have not really found anything concrete in the api documentation.

Ive tried the following code that results in an error. Fairly certain it is because i am trying to set the value before I add the Field.

featureLayer = new FeatureLayer(....);
fieldName = x;
value = y;  

featureLayer.graphics.attributes.fieldName = value;
  • ive also tried layer.graphics.setAttribute(fieldName,value); as well as layer.graphics[0].setAttribute(fieldName,value);
    – LCaraway
    Jan 6, 2016 at 23:04

2 Answers 2


See the JS API documentation for Graphic attr() method:


You would do this for each feature Graphic in the layer, after loaded:

for (var i=0; i< featureLayer.graphics.length; i++) {
    var lookupId = featureLayer.graphics[i].attributes.myLookupFieldId;

       ... perhaps get values for x, y using lookupId ...


Note that this only adds the field to the members of the graphics collection in the map layer (on the fly, as you suggest). If the features are re-loaded, you would have to re-iterate and attach the data again.

You get the values from each graphic by referencing the esri attributes property:

var thisValue = thisGraphic.attributes[x];

    or, assuming x == "newFieldName"

var thisValue = thisGraphic.attributes.newFieldName; 

Best, Bill B

  • Thanks for your input @Bill B. This makes sense. Could you clarify this line of Code < var lookup = featureLayer.graphics[i].attributes.myLookupField>. Specificly the "mylookupField" portion?
    – LCaraway
    Jan 7, 2016 at 15:17
  • It was my assumption that the value you are assigning (y) to each feature was unique for that feature. This was my attempt to suggest you use a field from the each feature (e.g. record id) to lookup/get the appropriate y value from your db/JSON data that you mentioned. In this case replace 'myLookupField' with '.id', or '.recordId' as appropriate from the existing feature fields.
    – Bill B
    Jan 8, 2016 at 16:50

Here is code that I found worked for this situation. Essentially, the code (not shown here) grabs the response of a POST method and parses it into javaScript variables. The bellow code injects the desired data into a feature layer.

addResponseData = function addResponseData (layer,fieldname,value) {
    for (feature in layer.graphics) {
       var attributes = layer.graphics[feature].attributes;
       attributes[fieldName] = value;

I can then call this function and plug in the desired map layer, new field name, and its value.

addResponseData(FeatureLayer,"FieldName", Value);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.