Javascript Object notation with variable

后端 未结 1 1758
傲寒
傲寒 2020-12-21 06:55

My PHP script specs.php outputs the following:

{
    \"hd\": {
        \"dimensions\": [
            \"1920x1080\",
            \"1920x1080\",
            \"         


        
相关标签:
1条回答
  • 2020-12-21 07:39

    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];
    
    0 讨论(0)
提交回复
热议问题