2

Note: it's been pointed out that some possible solutions were already proposed here. However, .map is exactly what I was looking for and doesn't appear in that otherwise-comprehensive post. Thomas's answer below covers my use case.

Using javascript, how can I make an array of values that represents an arbitrary property value for each feature in a GeoJSON file? I realize I'm failing JSON 101 here, but how can I create this:

['caiparinha','caucasian','margarita']

from this?

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {
        "name": "brasil",
        "beverage": "caiparinha"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          -56.953125,
          -8.754794702435605
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "name": "russia",
        "beverage": "caucasian"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          39.0234375,
          57.136239319177434
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "name": "mexico",
        "beverage": "margarita"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          -105.1171875,
          25.165173368663954
        ]
      }
    }
  ]
}

If the source were a CSV, it would be as easy as selecting/parsing a single column from the attributes, but with GeoJSON the properties (i.e. columns) are so deeply-nested that I haven't had any luck with .map, and I'd like to avoid looping, but I'll take anything at this point.

Context is that I'd like to be able to extract distribution information from each "variable" attached to the features I'm mapping, for example.

Community
  • 1
  • 1
Bill Morris
  • 589
  • 8
  • 16
  • By "arbitrary", do you mean "random"? – Bergi Sep 29 '14 at 00:40
  • I don't see why `responseData.features.map(function(f){return f.properties.beverage})` wouldn't work. What exactly had you tried? – Bergi Sep 29 '14 at 00:41
  • I hadn't used the function structure (i.e. I was doing this: `responseData.features.map.properties.beverage`). You're totally right. – Bill Morris Sep 29 '14 at 01:06
  • You can add the `.map` possibility yourself to that [post](http://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json/11922384#11922384): click the "edit" link at the bottom of that post. You can also add your own answer to that question suggesting using ".map()". – Peter O. Sep 29 '14 at 09:54

1 Answers1

4

Try something like below

var geojson = {
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "properties": {
        "name": "brasil",
        "beverage": "caiparinha"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          -56.953125,
          -8.754794702435605
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "name": "russia",
        "beverage": "caucasian"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          39.0234375,
          57.136239319177434
        ]
      }
    },
    {
      "type": "Feature",
      "properties": {
        "name": "mexico",
        "beverage": "margarita"
      },
      "geometry": {
        "type": "Point",
        "coordinates": [
          -105.1171875,
          25.165173368663954
        ]
      }
    }
  ]
};

var expected_result = geojson.features.map(function (el) {
  return el.properties.beverage;
});

console.log(expected_result);

If your GeoJSON is coming from an Ajax call, so it's a string in first, juste use JSON.parse(yourGeoJSON)

Thomas Gratier
  • 2,311
  • 1
  • 14
  • 15