Extract field of featurelayer as object

955
6
Jump to solution
01-08-2021 10:56 AM
RabinSubedi
New Contributor III

Hello Everyone,

How do we extract all fields of featurelayer as an object? I tried this but didn't worked:

var fieldslist = featureLayer.fields;
document.getElementById("List").innerHTML = fieldslist[0].NLFID;
 
I require this to perform computations only using some of the fields.
Thank you in advance!
1 Solution

Accepted Solutions
JohnGrayson
Esri Regular Contributor

Blake's response gets you almost there: 

 

// LAYER QUERY //
const query = HIMLayer.createQuery();
query.set({
  where: "1=1",
  returnGeometry: false
});
// RETRIEVE ALL FEATURES FROM LAYER //
HINLayer.queryFeatures(query).then((featureSet) => {
  // FEATURES THAT SATISFY THE ABOVE QUERY //
  const features = featureSet.features;
  // CREATE LIST OF ATTRIBUTES FROM THE FEATURES //
  const attributeData = features.map(feature => {
    return feature.attributes;
  });
  // DO SOMETHING WITH LIST OF ATTRIBUTES //
  document.getElementById("List").textContent = attributeData;
});

 

If you're only interested in the OBJECTIDS, then check out the queryObjectIds() method.

View solution in original post

6 Replies
BlakeTerhune
MVP Regular Contributor

The fields property of FeatureLayer will return an array of field objects, each having a field's properties. I found this only to be available once the FeatureLayer's layerview-create event has fired.

 

featureLayer.on("layerview-create", function(event){
  console.log(featureLayer.fields);
  console.log(featureLayer.getField("NLFID"));
});

 

 

RabinSubedi
New Contributor III

Thanks @BlakeTerhune for your reply. I used layerview-create event and tried to test the result by printing NLFID value of the first feature, but it didn't work. Can you please take a look at it? Does the 'fieldsList' variable contain all the fields (let's say the whole attribute table)?

Thanks much!

 

<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
  <title>High Injury Network</title>
  <style>
    html, body, #viewDiv {
      padding: 0;
      margin: 0;
      height: 50%;
      width: 100%;
    }
  </style>
  
    <link rel="stylesheet" href="https://js.arcgis.com/4.18/esri/themes/light/main.css">
  <script src="https://js.arcgis.com/4.18/"></script>
  
  <script>  

    require([
        "esri/Map",
        "esri/views/MapView",
        "esri/layers/FeatureLayer",
        "esri/layers/GraphicsLayer",
        "esri/geometry/geometryEngine",
        "esri/Graphic"
      ], function (
        Map,
        MapView,
        FeatureLayer,
        GraphicsLayer,
        geometryEngine,
        Graphic
      ) {
        var Crashes = [1,2];
        var HINLayer = new FeatureLayer({
          url: "https://services1.arcgis.com/wb4Og4gH5mvzQAIV/arcgis/rest/services/HIN_Columbus/FeatureServer",
          outFields: ["*"],
          visible: true
        });

        var resultsLayer = new GraphicsLayer();

      var map = new Map({
        basemap: "topo-vector",
        layers: [HINLayer, resultsLayer]
      });

      var view = new MapView({
        container: "viewDiv",
        map: map,
        center: [-82.9, 39.99], // longitude, latitude
      zoom: 10
      });
      
HINLayer.on("layerview-create", function(event){
        var fieldsList= HINLayer.fields;
        document.getElementById("List").innerHTML = fieldsList[0].NLFID;
        
});
      
   var sortedList = Crashes.sort();
   var sortedString = sortedList.join("','");
   var whereClause = "VehCrashCo IN ('" + sortedString + "')";
   var query = HINLayer.createQuery();
          query.where = whereClause ;
         // query.outStatistics = [ sumCost];
          query.returnGeometry= true;
          
          return HINLayer.queryFeatures(query).then(function(results){
            
            resultsLayer.removeAll();
          
          var features = results.features.map(function (graphic) {
            graphic.symbol = {
              
              color: "darkorange"
            };
            
            return graphic;
          });
          resultsLayer.addMany(features);
          
          });

    });
</script>
</head>
<body>
  <div id="viewDiv"></div>
  <div id="List"></div>
</body>
</html>

 

0 Kudos
BlakeTerhune
MVP Regular Contributor

Oh, you want field values, not field names? You'll have to query the FeatureLayer and then do something with the result. In your case, if you only want to display a single value from the first feature returned. Something like this maybe? And you don't need the layerview-create event listener for query.

 

HINLayer.queryFeatures({
  start: 0,
  num: 1,
  returnGeometry: false,
  outFields: ["NLFID"],
})
.then(function(featureSet) {
  let listContent;
  if(featureSet.features.length > 0) {
    listContent = featureSet.features[0].attributes.NLFID;
  } else {
    listContent = "There are no records in HINLayer";
  }
  document.getElementById("List").textContent = listContent;
});

 

RabinSubedi
New Contributor III

Actually, I need to do a tradeoff analysis with the data of this layer and present the resulting features in map and charts. I need to perform multiple queries and filtering according to my task requirement, which might be possible with ArcGIS JS API but I am not familiar with it, so it will take me a lot of time. I was hoping to extract the whole attribute table in the form of an object like this:

attributeData = [ { "OBJECTID": 1, "NLFID": "CFRACR00125**C", "SUM_c_segm": 6.0, "SUM_w_segm": 11.0, "STREET_NAM": "FRANK RD"},
{ "OBJECTID": 2, "NLFID": "CFRACR00114**C", "SUM_c_segm": 5.0, "SUM_w_segm": 6.0, "STREET_NAM": "COURTRIGHT RD"},
{ "OBJECTID": 3, "NLFID": "CFRACR00602**C", "SUM_c_segm": 5.0, "SUM_w_segm": 7.0, "STREET_NAM": "TOWN ST"}
...
...
...

}]

After the analysis, I plan to pass the array of OBJECTID of resulting features to the query to display them in the map. Is it possible to extract all data in this way?

I am sorry I couldn't make it clear earlier and I apologize for your trouble.

Thank You!

0 Kudos
JohnGrayson
Esri Regular Contributor

Blake's response gets you almost there: 

 

// LAYER QUERY //
const query = HIMLayer.createQuery();
query.set({
  where: "1=1",
  returnGeometry: false
});
// RETRIEVE ALL FEATURES FROM LAYER //
HINLayer.queryFeatures(query).then((featureSet) => {
  // FEATURES THAT SATISFY THE ABOVE QUERY //
  const features = featureSet.features;
  // CREATE LIST OF ATTRIBUTES FROM THE FEATURES //
  const attributeData = features.map(feature => {
    return feature.attributes;
  });
  // DO SOMETHING WITH LIST OF ATTRIBUTES //
  document.getElementById("List").textContent = attributeData;
});

 

If you're only interested in the OBJECTIDS, then check out the queryObjectIds() method.

RabinSubedi
New Contributor III

It works, Thank you very Much @BlakeTerhune @JohnGrayson 

0 Kudos