skip to Main Content

I have a video timeline containing several videos and I’m trying to create popups to go along with each video

I am trying to get the popups to appear when the video plays and if there is more than one object in the corresponding array, go through them and then carry on with the video

I’ve tried getting the objects entries and looping over them but it gives me both as opposed to just one

let popups = {
  0: [{
      type: 'alert',
      text: 'Alert text',
    },
    {
      type: 'warning',
      text: 'Warning text',
    },
  ],
  1: [{
    type: 'caution',
    text: 'Caution text',
  }, ],
};

3

Answers


  1. Chosen as BEST ANSWER

    The way I figured this out was using the Object.keys() and Object.values() method and nesting forEach loops inside forEach loops.

    const keys = Object.keys(popups);
    const values = Object.values(popups);
    
    //iterate over all keys
    keys.forEach((key, keyIndex) => {
        //Make sure they videoIndex is the same as the keyIndex
        if (videoIndex == keyIndex) {
    
            //Iterating over all values
            values.forEach((value, valueIndex) => {
                if (videoIndex == valueIndex) {
                    //Going over the value to break it down even further
                    value.forEach((item, itemIndex) => {
                        //Getting the type and text to display
                        create_popup(item.type, item.text);
                    });
                }
            });
        }
    });
    

  2. If I understand your question, you just need a foreach inside a foreach:

    let popups = [
      [
         {
            type: 'alert',
            text: 'Alert text',
         },
        {
        type: 'warning',
            text: 'Warning text',
        },
    ],
      [
            {
                type: 'caution',
                text: 'Caution text',
            },
        ],
    ];
    
    // Show all objects
    popups.forEach((popup) => popup.forEach((entry) => console.log(entry)))
    Login or Signup to reply.
  3. The popups is an object so you first need to iterate it, there is the Object.entries() method in js.

    let popupsToDisplay = [];
    Object.entries(popups).forEach(([key, value]) => {
        value.forEach(innerObject => popupsToDisplay.push(innerObject))
    })
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search