如何在Handlebarsjs underscorejs中按字母顺序显示姓名 [英] how to display names in alphabetical groups in Handlebarsjs underscorejs

查看:219
本文介绍了如何在Handlebarsjs underscorejs中按字母顺序显示姓名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一系列名称,电子邮件,电话等联系人,如下所示:

  [
{
name:Bob Wolmer,
email:bob@wolmer.com,
phone:(535)235-1234,
address:301 Cobblestone Wy。,Berdrock 00000,
contactId:1121,
标签:{}
},
{
name:Wilma Erra,
email:wilma@erra.com,
phone:(535)235-3659,
address:301 Cobblestone Wy。,Berdrock 70777,
contactId:1122,
标签:{}
},
{
name:Brad,
email:brad@brad.com,
phone :(535)235-3546,
address:301 Cobblestone Wy。,Redrock 70777,
contactId:1123,
labels:[ name:Friends},{name:Family}]
},
{
name:,
email: wilson@gmail.com,
电话:(535)235-3657,
地址: 301 Cobblestone Wy。,Dedrock 70777,
contactId:1124,
labels:[{name:朋友}]
}
]

我想显示如下名称

  B 
鲍勃
布拉德

W
威尔玛埃尔拉

任何一个人都可以告诉我如何实现这个目标。



在此先感谢

解决方案

让我们只说你已经将JSON分配给名为 contacts 的变量。下划线按名称属性的第一个字母分组,如下所示:

  var groupedContacts = _.groupBy(contacts,function(contact ){
return contact.name.substr(0,1);
});

然后,您可以遍历您的组,然后按照您的示例对它们进行排序并编写内容:

  _。each(groupedContacts,function(contacts,key){

console.log key); //写索引字母

//可选类型
var sortedContacts = _.sortBy(contacts,function(contact){
return contact.name;



$(写到联系人名称
console.log(contact.name);
});
});

所以在真实世界的应用程序中,您会想要替换控制台。使用Handlebars模板记录行和/或把它放在帮助器中(但是要注意在JavaScript中嵌入HTML字符串),但这很容易。另外,您可能希望对 groupedContacts 进行排序,以便按顺序获取索引。你可以使用上面例子中显示的相同的 sortBy 方法来做到这一点。


Hi am New to Handlebarsjs.

I have a collection of contacts with name, email, phone, etc. as below

[
  {
    "name": "Bob Wolmer",
    "email": "bob@wolmer.com",
    "phone": "(535) 235-1234",
    "address": "301 Cobblestone Wy., Berdrock 00000",
    "contactId": "1121",
    "labels": {}
  },
  {
    "name": "Wilma Erra",
    "email": "wilma@erra.com",
    "phone": "(535) 235-3659",
    "address": "301 Cobblestone Wy., Berdrock 70777",
    "contactId": "1122",
    "labels": {}
  },
  {
    "name": "Brad",
    "email": "brad@brad.com",
    "phone": "(535) 235-3546",
    "address": "301 Cobblestone Wy., Redrock 70777",
    "contactId": "1123",
    "labels": [{"name": "Friends"},{"name": "Family"}]
  },
  {
    "name": "",
    "email": "wilson@gmail.com",
    "phone": "(535) 235-3657",
    "address": "301 Cobblestone Wy., Dedrock 70777",
    "contactId": "1124",
    "labels": [{"name": "Friends"}]
  }
]

I want to display the names as below

B
Bob
Brad

W
Wilma Erra

Can any one suggest me how to achieve this.

Thanks in Advance

解决方案

Lets just say that you have assigned that JSON to a variable named contacts You can use underscore to group by the first letter of the name property like so:

var groupedContacts = _.groupBy(contacts, function(contact){ 
    return contact.name.substr(0,1); 
});

You could then iterate through your groups, maybe sort them and write the content as per your example like this:

_.each(groupedContacts, function (contacts, key) {

    console.log(key); // writes the Index letter

    // optional sort
    var sortedContacts = _.sortBy(contacts, function (contact) {
         return contact.name;
     });

     _.each(sortedContacts, function(contact) {
         // Writes the contact name
         console.log(contact.name); 
     });
});

So in a real world application, you would want to replace the console.log lines with Handlebars templates and/or put it in a helper (however be wary of embedding HTML string in JavaScript as a rule), but thats the easy bit. Also, you may wish sort your groupedContacts to get the index in order too. You can do this using the same sortBy method shown in my example above.

这篇关于如何在Handlebarsjs underscorejs中按字母顺序显示姓名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆