简体   繁体   English

显示用玉逗号分隔的数组?

[英]Displaying an array comma-separated in jade?

I want to display an array in Jade separated by commas instead of line which I currently have how can I do this?我想在 Jade 中显示一个用逗号分隔的数组,而不是我目前拥有的行,我该怎么做? This is segment of code I need help with players being the array passed from javascript这是我需要帮助的代码段,玩家是从 javascript 传递的数组

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

Assuming that players is an array of String 's you can use following statement: 假设playersString的数组,您可以使用以下语句:

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

That will give you something like (assuming you had 3 entries in the array: player1 , player2 and player3 ): 这会给你类似的东西(假设你在数组中有3个条目: player1player2player3 ):

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

I hope that will help. 我希望这会有所帮助。

Supposed you were working with not plain String arrays and your data is something like below: 假设你使用的不是普通的String数组,你的数据如下所示:

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

Then you could probably just use: 然后你可能只是使用:

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

NOTE: Not really the cleanest solution out there but does the job. 注意:并不是真正最干净的解决方案,但能完成工作。 Use at your own disposal. 请自行使用。 :) :)

If you have an array of objects and you want a comma-separated list of a property of each object, I went with:如果你有一个对象数组,并且你想要一个以逗号分隔的每个 object 的属性列表,我选择了:

- var playerList = players.map(player => player.name).join(', ')
p Currently playing: #{playerList}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM