Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

162
Visualizações
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
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda