admin管理员组文章数量:1414621
My PHP script specs.php outputs the following:
{
"hd": {
"dimensions": [
"1920x1080",
"1920x1080",
"1920x1080"
],
"sizes": [
"603 KB",
"265 KB",
"438 KB"
]
},
"medium": {
"dimensions": [
"800x530",
"800x530",
"800x530"
],
"sizes": [
"198 KB",
"105 KB",
"152 KB"
]
},
"status": "success"
}
With jQuery I load in the JSON and assign it to specs_obj
I can access the first item's "medium" "sizes" with specs_obj. medium.sizes[0]
How can I use a variable in the dot notation?
var specs_obj;
$.post("specs.php", {},
function(data) {
if (data.status == "success") {
specs_obj = data;
writeSizes("medium");
} else {}
}, "json"
);
function writeSizes(preset) {
// test get medium dimensions from first file
var size = specs_obj. medium.sizes[0];
// var size = specs_obj.preset.sizes[0];
}
My PHP script specs.php outputs the following:
{
"hd": {
"dimensions": [
"1920x1080",
"1920x1080",
"1920x1080"
],
"sizes": [
"603 KB",
"265 KB",
"438 KB"
]
},
"medium": {
"dimensions": [
"800x530",
"800x530",
"800x530"
],
"sizes": [
"198 KB",
"105 KB",
"152 KB"
]
},
"status": "success"
}
With jQuery I load in the JSON and assign it to specs_obj
I can access the first item's "medium" "sizes" with specs_obj. medium.sizes[0]
How can I use a variable in the dot notation?
var specs_obj;
$.post("specs.php", {},
function(data) {
if (data.status == "success") {
specs_obj = data;
writeSizes("medium");
} else {}
}, "json"
);
function writeSizes(preset) {
// test get medium dimensions from first file
var size = specs_obj. medium.sizes[0];
// var size = specs_obj.preset.sizes[0];
}
Share
Improve this question
asked Nov 23, 2010 at 11:07
TunaFFishTunaFFish
11.3k34 gold badges98 silver badges137 bronze badges
1 Answer
Reset to default 8You can't use a variable in the dot notation, but you can use brackets notation:
var size = specs_obj[preset].sizes[0];
If preset
contains the string "medium", that's functionally identical to:
var size = specs_obj.medium.sizes[0];
本文标签: Javascript Object notation with variableStack Overflow
版权声明:本文标题:Javascript Object notation with variable - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745150335a2644891.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论