1

I have the following JSON ("name" has more members only showing "cinema" here for simplicity)

{
    "name": {
        "cinema": {
            "size": {
                "w": 256,
                "h": 200
            },
            "frame": {
                "x": 0,
                "y": 0,
                "w": 256,
                "h": 200
            }
        }
     }
}

Which has been parsed using JSON.parse and stored in the varable bts_json. I want to loop through each member of "name" and detect if it has the member "frame". Below is my code, I get nothing printed on the console.

buildingNames = bts_json.name;

for (buildingFrame in buildingNames) {
   if (buildingFrame.hasOwnProperty("frame")) {
          console.log('exists');
          console.log(buildingFrame["frame"]["y"]);
    }
}

Where am I going wrong?

Thanks for helping :)

1
  • You might want a var in that for...in statement. for (var buildingFrame in buildingNames) { Commented Feb 10, 2013 at 12:56

1 Answer 1

3

You won't get the object, but the property name in buildingFrame, so you need to make it work like

if (buildingNames[ buildingFrame ].hasOwnProperty("frame")) {
}
Sign up to request clarification or add additional context in comments.

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.