Taking an element inside an object

Asked

Viewed 352 times

1

Hello, how do I get the "name" element, being that it is within "show" and the whole "show" is within "0"? (NOTE: If the nomenclature is wrong, I’m sorry, I’m beginner in JS)

0:
score: 31.86105
show:
externals: {tvrage: 18164, thetvdb: 81189, imdb: "tt0903747"}
genres: (3) ["Drama", "Crime", "Thriller"]
id: 169
image: {medium: "http://static.tvmaze.com/uploads/images/medium_portrait/0/2400.jpg", original: "http://static.tvmaze.com/uploads/images/original_untouched/0/2400.jpg"}
language: "English"
name: "Breaking Bad"//quero por em uma variavel
network: {id: 20, name: "AMC", country: {…}}
officialSite: "http://www.amc.com/shows/breaking-bad"
premiered: "2008-01-20"
rating: {average: 9.3}
runtime: 60
schedule: {time: "22:00", days: Array(1)}
status: "Ended"
summary: "<p><b>Breaking Bad</b> follows protagonist Walter White, a chemistry teacher who lives in New Mexico with his wife and teenage son who has cerebral palsy. White is diagnosed with Stage III cancer and given a prognosis of two years left to live. With a new sense of fearlessness based on his medical prognosis, and a desire to secure his family's financial security, White chooses to enter a dangerous world of drugs and crime and ascends to power in this world. The series explores how a fatal diagnosis such as White's releases a typical man from the daily concerns and constraints of normal society and follows his transformation from mild family man to a kingpin of the drug trade.</p>"
type: "Scripted"
updated: 1558526237
url: "http://www.tvmaze.com/shows/169/breaking-bad"
webChannel: null
weight: 93
_links: {self: {…}, previousepisode: {…}}
__proto__: Object

xhr.open('GET', `http://api.tvmaze.com/search/shows?q=${serie}`)

    xhr.addEventListener('load', function(){
        var api = (xhr.responseText)
        var recebidos = JSON.parse(api)
            console.log(recebidos)
        });

    xhr.send()

2 answers

1


Nice that you are learning Javascript, so for you to access elements within an array you have to indicate your index, that is, in the case how all the return of the API is within 1 array you can access so:

let dados = [
  {
    "score": 13.49527,
    "show": {
      "id": 8044,
      "url": "http://www.tvmaze.com/shows/8044/altersgluhen-die-serie",
      "name": "Altersglühen - Die Serie",
      "type": "Scripted",
      "language": "German",
      "genres": [
        "Romance"
      ],
      "status": "Ended",
      "runtime": 25,
      "premiered": "2014-11-13",
      "officialSite": null,
      "schedule": {
        "time": "23:15",
        "days": []
      },
      "rating": {
        "average": null
      },
      "weight": 0,
      "network": {
        "id": 508,
        "name": "WDR",
        "country": {
          "name": "Germany",
          "code": "DE",
          "timezone": "Europe/Busingen"
        }
      },
      "webChannel": null,
      "externals": {
        "tvrage": 45397,
        "thetvdb": null,
        "imdb": null
      },
      "image": {
        "medium": "http://static.tvmaze.com/uploads/images/medium_portrait/29/73542.jpg",
        "original": "http://static.tvmaze.com/uploads/images/original_untouched/29/73542.jpg"
      },
      "summary": "<p>Seven women and six men, almost all beyond the 70, gather for speed dating. The special thing: 13 actors, 19 cameras - but no script and no repeat. Only on the basis of character profiles can be a a high-caliber ensemble of actors to intense encounters, supported by empathy and the art of improvisation. The television movie Altersglühen – Speed Dating für Senioren by Jan Georg Schütte told the Speed Dating as a whole. <b>Altersglühen - Die Serie</b> - cut from the same rotating material - concentrates per episode to a single character who accompanies them through the meeting.</p>",
      "updated": 1477191187,
      "_links": {
        "self": {
          "href": "http://api.tvmaze.com/shows/8044"
        },
        "previousepisode": {
          "href": "http://api.tvmaze.com/episodes/445117"
        }
      }
    }
  }
]

console.log(dados[0].show.name);

If you have other elements show.name you could access its elements by methods such as map() or foreach().

  • That’s just what I needed. Thank you very much!

  • Legal Thales, good studies!

0

The result of this request will be an object array containing all the information.
To iterate this array and print the name of each item you can do so:

api.forEach(item => console.log(item.show.name));

Browser other questions tagged

You are not signed in. Login or sign up in order to post.