Removing duplicates from an array

807
4
11-03-2014 08:37 PM
joepublic
New Contributor III

Robert, et al

On further testing (click on the 3rd button "city models") I see that there are duplicates in my object ids which are being used to create the countries layer.  This causing my application to run slow.  A look at the console.log of the featureCollection on line 300 rquery.run(function(error, featureCollection, response) will show what I mean

Is there a way to remove the duplicates (so there is only one objectid per country) before the layer is added to my map?

Spec attached

0 Kudos
4 Replies
KenBuja
MVP Esteemed Contributor

There are a couple of ways to do this, like this or this

0 Kudos
joepublic
New Contributor III

Thanks for the suggestions Ken.

But my question is for a specific case.

The problem is here: rquery.objectIds();... This creates an

array of opjectIds with dupes.

I am trying to remove the dupes before I pass the array to :

countries = L.geoJson(featureCollection).addTo(map);

0 Kudos
ReneRubalcava
Frequent Contributor

The above examples could be tweaked a little for that.

var unique = {};

var distinctFeatures = featuresCollection.features.filter(function(feature) {

  var oid = feature.properties.OBJECTID;

  if (!unique[oid]) {

    unique[oid] = oid;

    return true;

  }

  return false;

});

featureCollection.features = distinctFeatures;

That's a quick and dirty way of doing it based on the example Ken provided.

0 Kudos
joepublic
New Contributor III

Thanks Rene!!

I work with this and let you know if I still problems

Chris

0 Kudos