【问题标题】:Displaying an array comma-separated in jade?在jade中显示一个逗号分隔的数组?
【发布时间】:2014-03-09 11:58:18
【问题描述】:

我想在 Jade 中显示一个以逗号分隔的数组,而不是我目前拥有的行,我该怎么做?这是我需要帮助的代码段,玩家是从 javascript 传递的数组

p Currently playing:
  ul
    each theExit in players
      p #{theExit}

【问题讨论】:

  • 您能否举例说明players 的值?

标签: html node.js pug


【解决方案1】:

假设playersString 的数组,您可以使用以下语句:

p Currently playing: #{players.join(', ')}

这会给你类似的东西(假设你在数组中有 3 个条目:player1player2player3):

<p>Currently playing: player1, player2, player3</p>

我希望这会有所帮助。

【讨论】:

    【解决方案2】:

    假设您使用的不是普通的 String 数组,并且您的数据如下所示:

    {
      [_id: 1, name: 'player1'],
      [_id: 2, name: 'player2'],
      [_id: 3, name: 'player3'],
    }
    

    那么你可能只是使用:

    each player, index in players
      if index === players.length -1
        | #{players.name}
      else
        | #{players.name},
    

    注意:这不是最干净的解决方案,但可以完成工作。随意使用。 :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-08
      • 1970-01-01
      相关资源
      最近更新 更多