如何在玉中迭代多个对象

时间:2016-04-14 16:52:45

标签: javascript pug

这是我的代码:

extends ../layout
block content
  -
    projects = [ { "name": "testProject", "status": "Active", "github": "", "slack": "", "trello", "" }, { "name": "testProject", "status": "Active", "github": "", "slack": "", "trello", "" } ]

  div.container
    // Nav tabs
    ul.nav.nav-tabs(role='tablist')
      li.active(role='presentation')
        a(href='#active', aria-controls='active', role='tab', data-toggle='tab') Active
      li(role='presentation')
        a(href='#inactive', aria-controls='inactive', role='tab', data-toggle='tab') Inactive
    // Tab panes
    .tab-content
      #active.tab-pane.active(role='tabpanel')
        each project in projects
          #projects
            .media
              .media-left
                i.fa.fa-2x.fa-code.media-object
              .media-body
                h4.media-heading #{project.name}
                span.label.label-success #{project.status}
                span.label.label-warning Help Wanted
                .links
                  span.github
                    i.fa.fa-code-fork
                    a(href='#{project.github}') GitHub
                  span.slack
                    i.fa.fa-slack
                    a(href='#{project.slack}') Slack
                  span.trello
                    i.fa.fa-trello
                a(href='#{project.trello}') Trello

我需要迭代数组中的每个对象并使用jade显示每个对象。我不知道该怎么办! 我按照这些教程:

  

http://jade-lang.com/reference/code/   iterate over an array of objects in jade/pugjs

2 个答案:

答案 0 :(得分:2)

您的 JSON 对象似乎有点不对......试试这个

您的Json看起来像" trello","" 将其更改为" trello":&# 34;" 这将验证您的json对象,以便它可以在每个循环的玉器中工作。

以下是一个例子:

projects = [ 
{
  "name": "testProject",
  "status": "Active",
  "github": "",
  "slack": "",
  "trello": ""
}, 
{
  "name": "testProject",
  "status": "Active",
  "github": "",
  "slack": "",
  "trello": ""
} ]

只需将下方复制并复制到您的文件中就可以了!

 projects = [ { "name": "testProject", "status": "Active", "github": "", "slack": "", "trello": "" }, { "name": "testProject", "status": "Active", "github": "", "slack": "", "trello": "" } ]

答案 1 :(得分:0)

使用=符号访问属性:

-var projects = [{name: 'name1', link: "link1"}, {name: 'name2', link: 'link2'}]

each project in projects
  div
     a(href=project.link)= project.name

如果要将变量插入字符串,请使用#{var}表示法:

a(href='my-link-#{project.link}')= project.name