I'm currently utilizing the TMDB API for fetching movie details and I want to extract the trailer from YouTube. If my JSON data appears as follows...
{
id: 568124,
results: [
{
iso_639_1: "en",
iso_3166_1: "US",
name: "What Else Can I Do?",
key: "PnJY20UCH9c",
site: "YouTube",
size: 1080,
type: "Clip",
official: true,
published_at: "2021-12-13T21:54:56.000Z",
id: "61b7d50b037264001cadc6e1",
},
{
iso_639_1: "en",
iso_3166_1: "US",
name: "Official Trailer",
key: "CaimKeDcudo",
site: "YouTube",
size: 1080,
type: "Trailer",
official: true,
published_at: "2021-09-29T13:00:05.000Z",
id: "615570dd07e281008dac4a0e",
},
],
};
How do I specifically fetch the KEY from the video identified by the NAME 'OFFICIAL TRAILER'. Currently, I can retrieve the first result ([0]) from the list using the code below...
let movieTrailerUrl = data.videos.results[0].key;
document.getElementById('movie-trailer').innerHTML +=
`<div class="video-responsive"><iframe width="560" height="315" src="https://www.youtube.com/embed/${movieTrailerUrl}" title="${movieTitle}" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe></div>`;
However, I need to ensure that the video selected from the JSON data is the one labeled 'OFFICIAL TRAILER'. Is there a way to extract the key only when the 'name' matches 'Official Trailer'?