5

I'm working with a GeoJSON dataset that is formatted like this:

{
  "type": "Feature",
  "properties": {
    "startcong": "109",
    "district": "7",
    "statename": "Pennsylvania",
    "member": {
      "112": {
        "21168": {
          "party": "Republican",
          "name": "Meehan, Pat",
          "district": "7"
        }
      },
      "109": {
        "15447": {
          "party": "Republican",
          "name": "Weldon, Curt", "district": "7"}
        },
      "110": {
        "20744": {
          "party": "Democrat",
          "name": "Sestak, Joe",
          "district": "7"
        }
      },
      "111": {
        "20744": {
          "party": "Democrat",
          "name": "Sestak, Joe",
          "district": "7"
        }
      }
    },
    "endcong":
    "112",
    "id": "042109112007"
  }
}

I'm struggling with how to access these nested objects. For instance, I can use feature.properties.member[112][21168] to access the party attribute. However:

  • That second numbered object ("21168") is not always consistently numbered.
  • That second numbered object is always the only nested object.

Is there a way to access that same party attribute using something like a wildcard? Ideally, something akin to feature.properties.member[112][*].party.

Cameron Scott
  • 1,276
  • 2
  • 17
  • 37

1 Answers1

7

If the second number is the only nested object, you can find what the number is using the builtin Object.keys(obj), something like:

var obj = feature.properties.member[112],
    key = Object.keys(obj)[0],
    party = obj[key].party

Sadly there is no wildcards for property accessing, but you can find what the property names are fairly simply.

Eric Ferreira
  • 1,811
  • 18
  • 20