Reputation: 2634
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 :)
Upvotes: 1
Views: 58
Reputation: 236192
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")) {
}
Upvotes: 3