【问题标题】:Access specific index of array with Pug使用 Pug 访问数组的特定索引
【发布时间】:2021-08-13 16:30:50
【问题描述】:

如何使用 Pug 在数组中显示特定项目?例如:

      each answer in answers
         li!= answer.Response

将显示数组中的每个项目。但是,假设我只想要第三项,或者更好的是,传递一个变量以显示特定索引。这是什么语法?

【问题讨论】:

  • 你试过answers[0]吗?
  • @tsvetanGanev 好主意,我试过并得到错误'无法读取属性'Answer' of null'

标签: node.js express pug


【解决方案1】:
- const indexIwant = 2;
if answers && answers.length>indexIwant
  li=answers[indexIwant]

您需要确保answers 不为空,并且至少具有包含所需索引项的项目数。

另一件事:除非您确切知道自己在处理什么数据,否则不要使用!=

【讨论】:

  • @user3203772 这能回答你的问题吗?
猜你喜欢
  • 2017-06-07
  • 2021-03-29
  • 2018-01-26
  • 2011-04-23
  • 1970-01-01
  • 1970-01-01
  • 2019-11-11
  • 2016-05-21
  • 2018-02-06
相关资源
最近更新 更多