1

I have a geojson file of the following structure:

var areas = {
"type": "FeatureCollection",                                                                                
"features": [
{ "type": "Feature", "id": 0, "properties": { "name": "a", "count": "854" }, "geometry": { "type": "Polygon", "coordinates": [ [ [ 13.271687552328165, 29.359549285088008 ], [ 13.272222904657671, 29.357697459147403 ], [ 13.272586765837973, 29.35566049412985 ], [ 13.273062097784726, 29.354438105832578 ], [ 13.272652418199639, 29.360795108476978 ], [ 13.271320041078822, 29.360700647951568 ], [ 13.271687552328165, 29.359549285088008 ] ] ] } }
,
{ "type": "Feature", "id": 1, "properties": { "name": "b", "count": "254"}, "geometry": { "type": "Polygon", "coordinates": [ [ [ 13.277038163109875, 29.358442424220023 ], [ 13.276782949188294, 29.358122923383512 ], [ 13.275290999273452, 29.358508600578681 ], [ 13.274634727185679, 29.358485484466968 ], [ 13.282581930993208, 29.358635779719847 ], [ 13.278334024184868, 29.359814295404375 ], [ 13.277038163109875, 29.358442424220023 ] ] ] } }
,
{ "type": "Feature", "id": 2, "properties": {"name": "c", "count": "385"}, "geometry": { "type": "Polygon", "coordinates": [ [ [ 13.279484097462499, 29.349831113628788 ], [ 13.27792879702741, 29.349728966688758 ], [ 13.276089401418951, 29.349901260351807 ], [ 13.275677565886326, 29.349951087700632 ], [ 13.279484097462499, 29.349831113628788 ] ] ] } }
,
{ "type": "Feature", "id": 3, "properties": { "name": "d", "count": "243"}, "geometry": { "type": "Polygon", "coordinates": [ [ [ 13.290520299215724, 29.352306689134622 ], [ 13.289722088408338, 29.351802774685929 ], [ 13.289065241087885, 29.352101541350635 ], [ 13.28785814146197, 29.351114667998772 ],  [ 13.290520299215724, 29.352306689134622 ] ] ] } }

]
}

Normally, to convert this data into an array I would create a loop to run through the appropriate variable storing this value in an array. However, I don't know if this is the best approach. Is there a way of pulling out all of the values for id into an array so that I would end up with:

[0,1,2,3]

I'm happy to use any external library to do this either - I'm mainly curious if there is a more efficient way than my current approach.

Chase
  • 29,019
  • 1
  • 49
  • 48
djq
  • 14,810
  • 45
  • 122
  • 157

3 Answers3

1

If you're absolutely desperate to give something else a go, you could try underscore's map. The code would look something like this:

_.map(areas.features, function (item) { return item.id });

This will call the inner function on each item in features, and return an array containing each result.

I don't think there's anything too radical you can do here - ultimately, pulling values out of nested objects and arrays can't be dumbed down all too much.

Bubbles
  • 3,795
  • 1
  • 24
  • 25
1

Not my code but there is a post on here regarding getting values from json quite efficiently.

refer to post use jQuery's find() on JSON object

Answer from User Box9 is probably the best way

function getObjects(obj, key, val) {
    var objects = [];
    for (var i in obj) {
        if (!obj.hasOwnProperty(i)) continue;
        if (typeof obj[i] == 'object') {
            objects = objects.concat(getObjects(obj[i], key, val));
        } else if (i == key && obj[key] == val) {
            objects.push(obj);
        }
    }
    return objects;
}

Upvoted this answer in regards to this code block as well because it's about the best way I've found to pull out values

Community
  • 1
  • 1
DRobertE
  • 3,478
  • 3
  • 26
  • 43
1

Something like this should work:

    var arr = new Array();
    for(var i = 0; i < areas.features.length; i++){
        arr.push(areas.features[i].id)
    }    

To get a certain id value directly you can do something like this:

areas.features[0].id

which in this case would be 0.

EXAMPLE

Chase
  • 29,019
  • 1
  • 49
  • 48