我正在使用 TMDB API 來獲取電影的詳細資訊,我需要從 Youtube 獲取預告片。如果我的 JSON 結果是這樣的......
{
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",
},
],
};
我怎樣才能只從標有“官方預告片”名稱的視頻中獲取 KEY。現在,我可以使用以下代碼從串列中獲取第一個結果([0])...
let movieTrailerUrl = data.videos.results[0].key;
document.getElementById('movie-trailer').innerHTML =
`<div ><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>`;
但我需要確保從 JSON 結果中選擇的唯一視頻是標記為“官方預告片”的視頻。是否可以執行諸如“僅當“名稱”等于“官方預告片”時才獲取密鑰之類的操作?
uj5u.com熱心網友回復:
使用Array.find要找到它的名稱一致節點。在這里,我考慮過您正在嘗試從名稱所在的陣列中找到一個專案"Official Trailer"。使用該匹配節點的名稱和標題來創建視頻鏈接
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 ><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>
uj5u.com熱心網友回復:
如果我理解正確,您只需要一把與官方預告片名稱匹配的鑰匙
// 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)
uj5u.com熱心網友回復:
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);
}
您可能還想將值小寫,例如。 trailer.name.toLowerCase() === 'Official Trailer'.toLowerCase()
uj5u.com熱心網友回復:
試試這個。
let movieTrailerUrl = data.videos.results.find((result) => result.key === 'What you want')
findArray 中的方法是回傳true在 array 中計算的第一個元素。
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find
uj5u.com熱心網友回復:
我的理解是你想從一組物件中獲取“官方預告片”物件
var obj = {
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",
},
],
};
console.log(obj.results.find(element => element.name === "Official Trailer"))
console.log(obj.results.find(element => element.name === "Official Trailer").key)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/409517.html
標籤:
