所以这是我的JSON数据;
{
"team_name": "New Team!",
"user_name": "hey_user",
"games": []
},
{
"team_name": "New Team!",
"user_name": "testing_user",
"games": []
},
{
"team_name": "Another Cool Team!",
"user_name": "test_user_2",
"games": []
}
我将此添加到我的.eco模板中:
<% for person in @persons: %>
<%= person.team_name %>
<%= person.user_name %> <br /> <br />
<% end %>
哪个输出:
New Team! hey_user
New Team! testing_user
但我希望它输出:
New Team! hey_user, testing_user, etc.
因此,如果有意义的话,团队名称不会继续出现在每个新用户身上。最好的方法来实现这个目标?
答案 0 :(得分:0)
如果我理解正确,您希望按团队对用户进行分组。您可以使用下划线的groupBy方法
var groups = _.groupBy(json, 'team');
它会给你这样的东西:
{
"New Team!": [
{
"team_name": "New Team!",
"user_name": "hey_user",
"games": []
},
{
"team_name": "New Team!",
"user_name": "testing_user",
"games": []
}
],
"Another Cool Team!": [
...
]
}
然后你可以将它传递给模板,迭代它并打印团队然后遍历内部用户数组。如果您需要数组而不是哈希,则可以在其上运行pairs。