0

I have an object like this :

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"
            }
        }
    }
}
];

I'm trying for some time to get the 1920x1080 value, but without success :

            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

I need your help. What should I do to get the 1920x1080 entry correctly ?

3 Answers3

1

Use bracket:

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']);
Psidom
  • 209,562
  • 33
  • 339
  • 356
0

Try this,

alert(obj[0].images.illustration['16x9']['1920x1080']);
Mohideen bin Mohammed
  • 18,813
  • 10
  • 112
  • 118
0

You should write next:

obj[0].images.illustration['16x9']['1920x1080']

In JS if you have object key with specific sympols you should wrap this in Square brackets ['1920x1080'], its like you get elemet from array.

Volodymyr Bilovus
  • 654
  • 2
  • 7
  • 20