1

I have a simple pug layout that takes an array of image source urls and an array of corresponding webpage urls, and I want to iterate over both at the same time. Essentially I want to do:

for ( i = 0; i < array.length; i++ ) {
  // display photos[i]
  // display webLinks[i]
}

I'm trying various things in pug, like

block content
    h1= title

    ul
        each val, link in photos, webLinks
            a(href=link)
                img(src=val width=200 height=150)

But this seems to only iterate over the photos array.

I've tried other things like

  each val in photos
  each link in webLinks
    // rest of code

This gives an error saying it didn't expect a newline.

I could pass pug a single object consisting of these arrays, if that would be easier. I don't see anything in the pug iteration documentation that addresses this issue.

1 Answer 1

3

Pug defines the index as the second argument in an each block.

You can achieve what you want with accessing the value of another array in the same each block using the index of the loop itself

ul
    each val, index in photos
        a(href=webLinks[index])
            img(src=val width=200 height=150)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.