2

I have an external JSON-file. It looks like this:

{
"type":"FeatureCollection",
"totalFeatures":1,
"features":
    [{ 
    "type":"Feature",
    "id":"asdf",
    "geometry":null,
    "properties":
        {
        "PARAM1":"19 16 11",
        "PARAM2":"31 22 11",
        "PARAM3":"12 10 7",
        }
    }],
"crs":null
}`

As I think "features" is an JSON-array and "properties" an object of this array. I stuck at trying to "push" the element "PARAM1" into another array in my JS-code. My attempt was to get the data via jQuery AJAX. It looks like this:

function (){
var arrPARAM1 = new Array ();
$.ajax({
    async: false,
    url: 'gew.json',
    data: "",
    accepts:'application/json',
    dataType: 'json',
    success: function(data) {
        arrPARAM1.push(data.features);
    }
})
console.log(arrPARAM1);
}

With arrPARAM1.push(data.features) I can "push" the whole array "features" into my JS-array. But I only want to have the element "PARAM1" from the object "properties". How can I get deeper (features --> properties --> PARAM1)?

Thanks for your attention!


SOLUTION:

arrPARAM1.push(data.features[0].properties.PARAM1);
1
  • never set async to false....its even deprecated (and has been for awhile) Commented Oct 21, 2015 at 18:00

4 Answers 4

4

It's just an array with a single element so access [0] then .properties

arrPARAM1.push(data.features[0].properties.PARAM1);
Sign up to request clarification or add additional context in comments.

Comments

1

You would be looking for something like this:

data.features[0].properties["PARAM1"]

Or

data.features[0].properties.PARAM1

Comments

0

You defined features as an array so when you access is, you must treat it as an array.

arrPARAM1.push(data.features[0].properties.PARAM1); //gets param1 from the features array

Comments

0

In case you have multiple features:

data.features.forEach(function(feature) {
    arrPARAM1.push(feature.properties.PARAM1);
})

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.