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

150
Views
Why is a value in an array only undefined when I set it in a function?

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?

about 4 years ago · Santiago Trujillo
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!