gpt4 book ai didi

javascript - 使用 javascript 测试 JSON 中是否存在属性

转载 作者:行者123 更新时间:2023-11-30 13:08:23 25 4
gpt4 key购买 nike

我有以下 JSON(“name”有更多成员,为了简单起见,这里只显示“cinema”)

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

已使用 JSON.parse 解析并存储在变量 bts_json 中。我想遍历“name”的每个成员并检测它是否有成员“frame”。下面是我的代码,我没有在控制台上打印任何内容。

buildingNames = bts_json.name;

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

我哪里错了?

感谢您的帮助:)

最佳答案

你不会得到object,而是buildingFrame中的property name,所以你需要让它像这样工作

if (buildingNames[ buildingFrame ].hasOwnProperty("frame")) {
}

关于javascript - 使用 javascript 测试 JSON 中是否存在属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14797926/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com