views:

189

answers:

2

I'we got a Json response from a ArcGIS server that look like this:

{  "displayFieldName" : "ELTTYPE", 
"features" : [
{
  "attributes" : {
    "ELTTYPE" : "Faldunderlag", 
    "DATANR" : 721301, 
    "ELEMENTNR" : 40, 
    "AREALTYPE" : "BELÆGNING", 
    "SHAPE.area" : 26.4595572
  }
}, 
{
  "attributes" : {
    "ELTTYPE" : "Prydplæne", 
    "DATANR" : 721301, 
    "ELEMENTNR" : 2, 
    "AREALTYPE" : "GRÆS", 
    "SHAPE.area" : 1993.23450096
  }
}, 
{
  "attributes" : {
    "ELTTYPE" : "Busket", 
    "DATANR" : 721301, 
    "ELEMENTNR" : 18, 
    "AREALTYPE" : "BUSKE", 
    "SHAPE.area" : 2105.69020834
  }
}...... and so on ]
}

I like to make a datagrid with the distinct values of ELEMENTNR and the summurized values of SHAPE.area.

Does any have an idea how to do this?

Sebastian

A: 

see Array.prototype.filter

You'll need to include the filter script snippet in order to utilize it for unsupported browsers..

function reduceMyData(input) {
  var check = {};
  return input.filter(function(item, index, ary){
    var id = item.attributes["ELEMENTNR"];
    if (check[id]) return false;
    return check[id] = true;
  });
}

var myFeatures = reduceMyData(data.features);
Tracker1
you can also use dojo.filter or dojo.map to set/transform properties like 'id'
peller
@peller, I'm not that familiar with dojo, only know that filter/map etc are included in many modern browsers, are part of the ES5 specification and have a script based implementation. I prefer to include native functionality where possible.
Tracker1
Unfortunately, not everyone is running a browser with the Javascript 1.6 methods, and nobody is running an ES5-compliant browser. Dojo used to map to the native methods. I forget why it doesn't anymore.
peller
@peller, that's why I specifically mentioned, there's a script implementation for other browsers (IE) that don't include filter/map etc.
Tracker1
A: 

As I understand you need not only to get elements with distinct ELENTNR, but also to accumulate SHAPE.area for the elements with the same ELENTNR. If so:

var codes = {};
// features - is an array of features from your json
var distinctFeatures = dojo.filter(features, function(m){
    if(typeof(codes[m.attributes.ELEMENTNR]) == "undefined"){
        codes[m.attributes.ELEMENTNR] = m.attributes["SHAPE.area"];
        return true;
    }
    else{ // if duplicate
        codes[m.attributes.ELEMENTNR] += m.attributes["SHAPE.area"];
        return false;
    }
});
for(var index in distinctFeatures){
    var elementNr = distinctFeatures[index].attributes.ELEMENTNR;
    distinctFeatures[index].attributes["SHAPE.area"] = codes[elementNr];
}
Kniganapolke