Javascript Object notation with variable - Stack Overflow

My PHP script specs.php outputs the following:{"hd": {"dimensions": ["1920x108

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
Add a ment  | 

1 Answer 1

Reset to default 8

You 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];

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745150336a4613839.html

相关推荐

  • Javascript Object notation with variable - Stack Overflow

    My PHP script specs.php outputs the following:{"hd": {"dimensions": ["1920x108

    3小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信