javascript - 如何在 pug 中迭代 map

标签 javascript node.js pug

我有一张 map 要传递给服务器上的 pug,但我无法迭代 map 中的键值对以呈现 map 中的数据。我已经尝试简化 map ,但仍然无法使其工作。

router.get('url', middlware(),(req,res)=>{
  let renderData = {};
  renderData.test = new Map([["1","one"],["2","two"]]);
  res.render('template', renderData);
})

模板:

block content
  .container
    .mt-3.whiteBG
      each item in test
        p here is some text

在简化版本中,我什至无法渲染纯文本,更不用说在迭代中访问 map 的值了。如果我执行这样的操作,我就可以访问这些值 p #{test.get("1")} map 在 Pug 中是不可迭代的吗?如果没有,有没有办法将我的 map 转换为 Pug 可以迭代的数组/对象?

最佳答案

帕格将 support iteration over ES6 collections in the next release 。在此之前,您可以使用 Object.fromEntries() 将 Map 转换为对象。 :

renderData.test = Object.fromEntries(renderData.test);

关于javascript - 如何在 pug 中迭代 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58515423/

相关文章:

javascript - 如何过滤嵌套数组(组成JS过滤器)

javascript - 如何从 Node js 与 shell 脚本通信

javascript - 我们如何将数据发送到 Node js中与数据库调用异步的局部变量

javascript - 从 Node js中的MYSQL元素集中选择特定元素

javascript - 在 webview 中显示 android 原生日期选择器

javascript - 单击工具提示执行操作

Node.JS 包含 Jade 文件 errno : -2,

node.js - Jade : Are the template files loaded per npm start?

pug - 我必须在Jade中使用空间吗?

javascript - 使用 on() jQuery 方法将 'this' 作为参数传递给事件处理程序