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

126
Visualizações
How do I iterate through an inline var using pug

I am attempting to iterate through an array and each time add 1 to the inline pug variable. So far this is what I got.

ul(class="benchcards")
            - var i = -1
            each card in cards
                -var L = i + 1
                li
                    a.btn(href="/addmonster" + L)
                        div=card.name
                        div= "Attack: "+card.attack
                        div= "HP: "+card.hp
                        div= "Attribute: "+card.attribute
                        div= "Energy: "+card.energy

When I run the page "L" is always = to 0. I want it to add 1 each time. The question really comes down to what am I doing wrong?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

As said in the comments you are not incrementing the i, you can fix your issue like this:

ul.benchcards
  - var i = -1;
  each card in cards
    li
      a.btn(href='/addmonster' + i++)
        div= card.name
        div= 'Attack: ' + card.attack
        div= 'HP: ' + card.hp
        div= 'Attribute: ' + card.attribute
        div= 'Energy: ' + card.energy
about 4 years ago · Juan Pablo Isaza Relatório

0

It's better to use the index built into the each iterator in Pug. That way you don't have to declare or increment a variable.

ul.benchcards
  each card, i in cards
    li
      a.btn(href=`/addmonster${i}`)
        div #{card.name}
        div Attack: #{card.attack}
        div HP: #{card.hp}
        div Attribute: #{card.attribute}
        div Energy: #{card.energy}
about 4 years ago · Juan Pablo Isaza Relatório
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