0

I am adding Esri feature layer to Leaflet map from Esri REST API. I can access the feature properties, like this

feature.properties.CasesField

How can I iterate through this CasesField and get:

  1. All the values
  2. Sum the values
function initMap() {
  map = L.map("map", {
    zoomControl: true,

    zoomSnap: 0.25
  }).setView([33.1625662, -96.6390040], 5); //home position
  ;

  L.esri.basemapLayer("Topographic").addTo(map);

  casese = L.esri.featureLayer({
    url: casesURL,
    onEachFeature: function (feature, layer) {
      var props = feature.properties.CasesField;


      var sum = 0;
      for (x in props) {
        sum += props;
      }

      // use the value to do something...

      console.log(sum);

    }

  }).addTo(map);

}

4
  • What is the content of this feature.properties.CasesField property?
    – TomazicM
    Mar 24, 2020 at 6:43
  • @TomzicM its a number field, also would like access other string field as well. Mar 24, 2020 at 11:45
  • If it's single number field, why are you summing it in a loop? Or does it mean you want sum this one field across all the features?
    – TomazicM
    Mar 24, 2020 at 12:16
  • Yes, would like to sum this one field across all the features. Mar 24, 2020 at 12:19

1 Answer 1

1

You have to define sum variable outside feature processing function as global variable. Then you have to wait for feature layer load event before you can use/display sum.

Code could then look something like this:

var sum = 0;

var casese = L.esri.featureLayer({
  url: casesURL,
  onEachFeature: function (feature, layer) {
    sum += feature.properties.CasesField;
  }
}).addTo(map);

casese.on('load', function() {
  console.log(sum);
});
1
  • Thank you, Yes that it, it worked perfectly. Mar 24, 2020 at 20:36

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.