Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

213
Views
Javascript de JSON si el parámetro es verdadero

Estoy usando la API de TMDB para obtener los detalles de las películas y necesito obtener el tráiler de Youtube. Si mis resultados JSON son así...

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

¿Cómo puedo SOLAMENTE obtener la CLAVE del video marcado con el NOMBRE 'TRAILER OFICIAL'? En este momento, puedo obtener el primer resultado ([0]) de la lista con el siguiente código...

 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>`;

Pero debo asegurarme de que el único video elegido de los resultados de JSON sea el marcado como 'TRAILER OFICIAL'. ¿Es posible hacer algo como 'obtener la clave solo si el 'nombre' es igual a 'Tráiler oficial'?

about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

Si entiendo correctamente, solo necesita una clave que coincida con el nombre del tráiler oficial.

 // this is your data i just added in the variable but you don't need to do this const data = { 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", }, ], }; const {results } = data console.log(results) let key results.forEach((e) => { if(e.name.includes("Official Trailer")) { key = e.key } }) console.log(key)

about 4 years ago · Juan Pablo Isaza Report

0

Utilice Array.find para encontrar el nodo coincidente con su nombre. Aquí he considerado que está tratando de encontrar un elemento de la matriz donde el nombre es exactamente "Official Trailer" . Use el nombre y el título de ese nodo coincidente para crear el enlace de video

 const apiResponse = { 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", }, ], }; const movieTrailer = apiResponse.results.find(node => node.name.toUpperCase() === 'OFFICIAL TRAILER') const movieTrailerUrl = movieTrailer.key; const movieTitle = movieTrailer.name; // Mark the correct node 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>`;
 <div id="movie-trailer"></div>

about 4 years ago · Juan Pablo Isaza Report

0

const movieTrailer = data.videos.results.find((trailer) => trailer.name === 'Official Trailer'); // if statment needed because `.find` can return undefined in case it doesn't find the value if (movieTrailer) { console.log(movieTrailer.key); }

Probablemente también desee valores en minúsculas, por ejemplo. trailer.name.toLowerCase() === 'Official Trailer'.toLowerCase()

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!