Pug.js 在每个循环中使用空格键或空格访问 json 键

问题描述

我在 pug 中为每个循环访问带有空格或空格键的 json 键时遇到问题。反引号、单引号和双引号对我不起作用,所以我不得不回到这一点。

.pug 文件

for element in aJson
  for value,key in element
    if key == "my key with spaces"
      |  
      =value

有没有其他更干净的解决方法,或者我错过了什么?我在 .pug 文件中使用制表符进行缩进。我使用的是 3.0.0 版 (https://www.npmjs.com/package/pug)

以下不起作用:

.pug 文件

for element in aJson
  |  
  =element.`my key with spaces`
----> Syntax error

for element in aJson
  |  
  =element.my key with spaces
-----> "my" not found error

我的 json 文件如下所示:

{
  "Element": [
    {
      "my key with spaces": "Value 1","Key2": "Value 2","Key3": "Value 3","Key4": "Value 4","Key5": "Value 5"
    },],"Element2": [
    {
      "my key with spaces": "Value 1",...

}

解决方法

@Sean:谢谢你解决了我的问题。我知道您可以在 pug 中使用 =element[0],=element[1],... 使用索引函数,但不知道您也可以通过这种方式访问​​键。这是对我有用的代码

for element in aJson
  |  
  =element['my key with spaces']