I have an array consisting of objects, which each contains
let videos = [
{ link: 'https://www.youtube.com/watch?v=umW2AwxZGV0', title: 'title', thumbnail: "thumbnail" },
{ link: 'https://www.youtube.com/watch?v=CTxISFb84IU', title: 'title', thumbnail: "thumbnail" },
{ link: 'https://www.youtube.com/watch?v=Wn_Kb3MR_cU', title: 'title', thumbnail: "thumbnail" },
{ link: 'https://www.youtube.com/watch?v=lWAOefb5jCU', title: 'title', thumbnail: "thumbnail" },
{ link: 'https://www.youtube.com/watch?v=qj9KR7KMKFs', title: 'title', thumbnail: "thumbnail" }
];
Furthermore I have a function that returns the video-ID from a a youtube link.
function YouTubeGetID(url) {
url = url.split(/(vi\/|v=|\/v\/|youtu\.be\/|\/embed{\/)/);
let videoID = url[2] !== undefined ? url[2].split(/[^0-9a-z_\-]/i)[0] : url[0];
return videoID;
}
Using an npm package, I retrieve the title (code is down below).
for (let i = 0; i < videos.length; i++) {
let videoId = YouTubeGetID(videos[i].link);
getYoutubeTitle(videoId, ytApiKey, function (err, title) {
if (err) {
throw err;
}
videos[i].title = title;
})
videos[i].thumbnail = `http://img.youtube.com/vi/${videoId}/default.jpg`
}
With this for-loop i get the link from my array, get the id, and fetch the title.
If I console.log()
title
inside getYoutubeTitle()
I get the right title of the video. But if I want to set videos[i].title = title
no error is thrown. But if I log the videos array the title is not changed (a.k.a. the title is undefined).
Can someone help me?