Select to view content in your preferred language

Filter Array

3750
10
10-21-2015 10:40 AM
jaykapalczynski
Honored Contributor

I have a query that:

  1. is selecting features via a Buffers Geometry
  2. It grabs that array, pushed to a Function and does a Unique filter on that array
  3. It then returns it to a table created via JavaScript.

I am not seeing a fully unique return set....as you can see in the attachment there are two "sunfish, bluespotted" being returned even thought he COMMON_NAME field is the same as specified in the Unique Filter.

I am wondering if this is not working correctly?

do I have something set up wrong?

It is decreasing the number of returned records but does not seem to be getting all of unique values....I checked my data and this field seems to be unique, wondering if I am doing something wrong.

// SNIP.....

var query3 = new Query();

   query3.geometry = bufferGeometry;

  // Select the Points within the Buffer and show them

   featureLayerVAFWIS.selectFeatures(query3, FeatureLayer.SELECTION_NEW, function(results){

   });

   // Query for the records with the given object IDs and populate the grid

   featureLayerVAFWIS.queryFeatures(query3, function (featureSet) {

  updateGrid3(featureSet);

});

function updateGrid3(featureSet) {

  var data = arrayUtils.map(featureSet.features, function (entry, i) {

   return {

  id: entry.attributes.OBJECTID,

  ObsID: entry.attributes.ObsID,

  SppBova: entry.attributes.SppBova,

  COMMON_NAME: entry.attributes.COMMON_NAME,

  GENUS: entry.attributes.GENUS,

  SPECIES: entry.attributes.SPECIES,

  Tier: entry.attributes.Tier,

  FedStatus: entry.attributes.FedStatus,

  TaxaGrp: entry.attributes.TaxaGrp,

   };

  });

  var uniqueName = [];

  var UniquValues = array.filter(data, function (item) {

        var IsNameUnique = true;

       array.forEach(uniqueName, function (value) {

      if (value == (item.COMMON_NAME)) {

          IsNameUnique = false;

       }

       });

       if (IsNameUnique) {

         uniqueName.push(item.COMMON_NAME);

       }

       return IsNameUnique;

   });

    var i;

    var out = "<table id=t01>";

    for(i = 0; i < uniqueName.length; i++) {

        out += "<tr><td>" +

        arr.id +

        "</td><td>" +

        arr.ObsID +

        "</td><td>" +

        arr.SppBova +

        "</td><td>" +

        arr.COMMON_NAME +

        "</td><td>" +

        arr.GENUS +

        "</td><td>" +

        arr.SPECIES +

        "</td><td>" +

        arr.Tier +

        "</td><td>" +

        arr.FedStatus +

        "</td><td>" +

        arr.TaxaGrp +

        "</td></tr>";

    }

    out += "</table>";

    document.getElementById("id01").innerHTML = out;

  dom.byId('upload-function').innerHTML = "See Results below";

  domStyle.set(loadingShapefile, "display", "none");

}

0 Kudos
10 Replies
TracySchloss
Honored Contributor

I start with an empty array myArr = [].  Then as I step through the output of my result handler,  I check to see if a value is already in the array like this:

var pos = myArr.lastIndexOf(value);

if (pos == -1) {  //value wasn't found

myArr.push(value)

}

Only new values that aren't already in the array are added.

I'm not sure where this fits into your code.

0 Kudos