如果否则在哈巴狗的一个循环内

时间:2017-12-02 12:33:01

标签: loops express if-statement pug

这是Create variables inside an each loop in a pug template

的后续问题

我在哈巴狗的循环中有一个简单的if else的问题。 这是我的代码:

 extends layout
 block content
 h2 #{item}
 each word, index in words
  - var spelling = word.orth;
  if(item == spelling)
   p #{spelling}
  else
   p does not exist yet

问题是如果一个单词存在,它会正确打印出来但它还会打印 。如果一个单词不存在,则打印出尚不存在 3次(这是我正在搜索的xml文件中的单词数,见下文)

背景:我在xml文件中搜索单词。变量 item 是用户输入,我们在xml文件中查找的单词。

1 个答案:

答案 0 :(得分:1)

 extends layout
 block content
 h2 #{item}
 - var found = 0
 each word, index in words
  - var spelling = word.orth;
  if(item == spelling)
   - found = 1
   p #{spelling}
  else if (found == 0 && index == words.length -1)
   p does not exist yet