I am building an video saving service as a project and have an array of the user's saved videos obtained from the database response.array[], I then push the videoId into an display array video[] but on displaying the respective videoId using console.log() it keeps coming undefined.
I have the code snippet as below.
let video = [];
this.service.nextVideo().subscribe( response =>
{
    response.array.forEach(element => {
    video.push(element.videoId);
  });
})
console.log(video[0]); //Output is undefined
The full code snippet is like such
nxtVideo(){
    let video = [];
    this.service.nextVideo().subscribe( response => 
    {
        response.array.forEach(element => {
          console.log(element.videoId);//Display's video id
          video.push(element.videoId);
        });
    })
    console.log(video[0]); //Output undefined
    console.log(video); //Output shows all elements but on accessing specific elements it shows undefined
    return (video[this.offset++]); //offset is a global variable to measure watched videos
  }
  Launch(){
    let videoId = this.nxtVideo();
    //The Display table starts here
  }
 
    