javascript - 从对象中提取图像值

标签 javascript

我有一个像这样的对象:

var obj = [
{
    "id": 728,
    "title": "A long day",
    "images": {
        "illustration": {
            "title": "Preview",
            "16x9": {
                "1248x702": "https://example.com/image/225944d77559.jpg",
                "1920x1080": "https://example.com/image/4546b05422594.jpg"
            }
        }
    }
}
];

我正在尝试一段时间来获取 1920x1080值,但没有成功:

            alert(obj[0].title); //works 
            alert(obj[0].images.illustration.title); // works 
            alert(obj[0].images.illustration.16x9.1920x1080); // doesn't work and break the code
            alert(obj[0].images.illustration.'16x9'.'1920x1080'); // doesn't work and break the code

我需要你的帮助。我应该怎样做才能正确获取 1920x1080 条目?

最佳答案

使用括号:

var obj = [
{
    "id": 728,
    "title": "A long day",
    "images": {
        "illustration": {
            "title": "Preview",
            "16x9": {
                "1248x702": "https://example.com/image/225944d77559.jpg",
                "1920x1080": "https://example.com/image/4546b05422594.jpg"
            }
        }
    }
}
];

console.log(obj[0].images.illustration['16x9']['1920x1080']);

关于javascript - 从对象中提取图像值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45118830/

相关文章:

javascript - Meteor:OSM的Oauth1.0a登录服务

javascript - 为什么 Backbone.js 模型的 'on()' 将 'this' 作为最后一个参数,如果它几乎总是这样的话?

javascript - 为什么我不能返回仅包含此对象数组中某些键的对象数组?

javascript - 使用 AngularJs 和 ES6 类 : 'this' is not available in ng-click

javascript - 如何更改输入标签对 addEventListener ('change' 的响应)?

javascript - 使用 Jquery 将变量传递到链接的一部分

javascript - 防止中键单击按钮在 FIREFOX 中打开新选项卡不起作用

javascript - 使用变量创建正则表达式模式以替换点

javascript - Jquery 数据表服务器端分页与外部搜索字段

javascript - 在 javascript 常规模式中包含变量