2017-07-15 4 views
1

Ich habe ein Objekt wie folgt aus:Extract Bildwert von einem Objekt

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

Ich bin seit einiger Zeit versucht, die 1920x1080 Wert zu erhalten, aber ohne Erfolg:

  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 

Ich brauche deine Hilfe. Was muss ich tun, um den 1920x1080-Eintrag richtig zu bekommen?

Antwort

1

Verwenden Halter:

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']);

0

die Sie interessieren,

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

sollten Sie nächstes schreiben:

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

In JS wenn yo Wenn Sie einen Objektschlüssel mit spezifischen Symbolen haben, sollten Sie dies in eckige Klammern ['1920x1080'] einfügen, so wie Sie Elemente aus dem Array erhalten.