javascriptforeach

What is wrong with this JavaScript foreach loop?


var videos = {
    a: Popcorn("#a"),
    b: Popcorn("#b"),
    c: Popcorn("#c")
}

videos.forEach(function(vid){
    if (vid.media.readyState === 4) {
    vid.currentTime(
      videos.a.currentTime()
    );
  }
}); 

For some context, I am attempting to scale-up this JSFiddle: Synchronizing Playback of Two Videos so that I can play any number of videos simultaneously.

In addition, I am wondering if there is any way to apply the foreach loop to each element in the array EXCEPT for the element in the 0th index.


Solution

  • Objects don't have forEach Method, instead you can create array of object keys & loop through the keys to access all keys & check values like this

    Object.keys(videos).forEach((vid)=>{
        if (videos[vid].media.readyState === 4) {
            videos[vid].currentTime(
              videos.a.currentTime()
            );
          }
    });