Select to view content in your preferred language

Filter Array

3761
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
KenBuja
MVP Esteemed Contributor

Try filtering your array like this

function filterUnique(values){

    var unique = {};

    return dojo.filter(values, function(value) {

        if (!unique[value]) {

            unique[value] = true;

            return true;

        }

        return false;

    });

}

0 Kudos
jaykapalczynski
Honored Contributor

Sound I put that function outside the existing function and pass the array "data" to it?

0 Kudos
KenBuja
MVP Esteemed Contributor

Untested, but I think you could also do this

var UniqueValues = return array.filter(data, function(value) {
        if (!unique[value]) {
            unique[value] = true;
            return true;
        }
        return false;
});
0 Kudos
jaykapalczynski
Honored Contributor

I tried this and get error:  "unexpected Token Return"

I changed values to the array name data

var UniqueValues = return dojo.filter(data, function(value) {

        if (!unique[value]) {

            unique[value] = true;

            return true;

        }

        return false;

});

0 Kudos
KenBuja
MVP Esteemed Contributor

Sorry, I was updating the code when you copied it out.

0 Kudos
jaykapalczynski
Honored Contributor
  1. If I use your latest example I get this error:  Uncaught SyntaxError: Unexpected token return
  2. If I remove the "return" as seen below I get this error:   ReferenceError: unique is not defined(…)

Am I missing a reference to something?

My other issue is that I am trying to get the unique records from 1 or 2 fields...not the entire item list.

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 UniqueValues = array.filter(data, function(value) {

        if (!unique[value]) {

            unique[value] = true;

            return true;

        }

        return false;

});

0 Kudos
jaykapalczynski
Honored Contributor

Still getting errors from the "return" from your code so I tried it this way but not getting Unique values....

var newarr = [];

var unique = {};

dojo.forEach(data, function(item) {

    if (!unique[item.GENUS + item.SPECIES]) {

        newarr.push(item);

        unique[item.GENUS + item.SPECIES] = item;

    }

});

0 Kudos
thejuskambi
Frequent Contributor

Hello jay

i wa look at the part where you build the html,and was not able to understand where arr came from. try looking at that variable and see if something is wrong there.

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;

0 Kudos
jaykapalczynski
Honored Contributor

Think I may have it....another colleague pointed something out...have to test and then will post all code.

0 Kudos