jade 中的 Foreach 循环(node.js 模板引擎)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12366402/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 16:17:20  来源:igfitidea点击:

Foreach loop in jade (node.js template engine)

node.jsforeachassociative-arraypug

提问by Sagiv Ofek

Ok, I am getting an associative array from node server and trying to render it in Jade. I obviously need a foreach loop, but nothing seems to work! I tried these both codes:

好的,我从节点服务器获取关联数组并尝试在 Jade 中呈现它。我显然需要一个 foreach 循环,但似乎没有任何效果!我试过这两个代码:

- foreach row in rows {
    li= row
- }

and

- rows.forEach(function(item)) {
    li= item
- })

the array I am passing is called "rows". Any idea why this is not working? I am getting this error:

我传递的数组称为“行”。知道为什么这不起作用吗?我收到此错误:

500 SyntaxError: Unexpected identifier

and, with the second code:

并且,使用第二个代码:

500 SyntaxError: Unexpected token )

回答by Sagiv Ofek

try

尝试

each item in rows
    li= item

回答by frontsidebus

Your second example would work except you have a small syntax error in it - an extra parentheses, it should be:

你的第二个例子可以工作,除非你有一个小的语法错误 - 一个额外的括号,它应该是:

- rows.forEach(function(item) {
  li= item
- })

回答by Mostafa Nawara

You can use

您可以使用

ul
  each val, index in ['zero', 'one', 'two']
    li= index + ': ' + val

or

或者

ul
  each val, index in {1:'one',2:'two',3:'three'}
    li= index + ': ' + val

see this link

看到这个链接