js通讯录按照首字母排序

5 篇文章 0 订阅
4 篇文章 0 订阅
this._sortFriend(data.data)
_chineseToEnglish(c) {
    var idx = -1;
    var MAP = 'ABCDEFGHJKLMNOPQRSTWXYZ';
    var boundaryChar = '驁簿錯鵽樲鰒餜靃攟鬠纙鞪黁漚曝裠鶸蜶籜鶩鑂韻糳';
    if (!String.prototype.localeCompare) {
        throw Error('String.prototype.localeCompare not supported.');
    }
    if (/[^\u4e00-\u9fa5]/.test(c)) {
        return c;
    }
    for (var i = 0; i < boundaryChar.length; i++) {
        if (boundaryChar[i].localeCompare(c, 'zh-CN-u-co-pinyin') >= 0) {
            idx = i;
            break;
        }
    }
    return MAP[idx];
},
//获取第一个字母
_getFirstUpperChar(str) {
    var string = String(str);
    var c = string[0];
    if (/[^\u4e00-\u9fa5]/.test(c)) {
        return c.toUpperCase();
    } else {
        return this._chineseToEnglish(c);
    }
},
//排序
_sortFriend(list) {
    let that = this
    let map = {}
    map['#'] = [];
    var c = 'A'.charCodeAt();
    for (; c <= 'Z'.charCodeAt(); c++) {
        map[String.fromCharCode(c)] = [];
    }
    var firstCharUpper;
    list.forEach(function (item) {
        firstCharUpper = that._getFirstUpperChar(item.name);
        if (map.hasOwnProperty(firstCharUpper)) {
            map[firstCharUpper].push(item);
        } else {
            map['#'].push(item);
        }
    });
    for (let a in map) {
        if (!map[a].length) {
            delete map[a]
        }
    }
    return map;
}

记录小功能

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现通讯录按名字首字母排序分组,可以按照以下步骤进行: 1. 获取通讯录数据,并将每个联系人的名字转换为拼音。 2. 根据拼音首字母对联系人进行分组。 3. 对每个分组内的联系人按照名字进行排序。 4. 在页面上渲染每个分组及其对应的联系人。 以下是一个简单的实现示例: 1. 使用 pinyin 库将联系人名字转换为拼音: ```js import pinyin from 'pinyin'; const contacts = [ { name: '张三', phone: '123456789' }, { name: '李四', phone: '987654321' }, // ... ]; const pinyinContacts = contacts.map(contact => { const pinyinName = pinyin(contact.name, { style: pinyin.STYLE_FIRST_LETTER }).join(''); return { ...contact, pinyinName }; }); ``` 2. 根据拼音首字母对联系人进行分组: ```js const groupedContacts = pinyinContacts.reduce((groups, contact) => { const firstLetter = contact.pinyinName[0].toUpperCase(); if (!groups[firstLetter]) { groups[firstLetter] = []; } groups[firstLetter].push(contact); return groups; }, {}); ``` 3. 对每个分组内的联系人按照名字进行排序: ```js Object.keys(groupedContacts).forEach(key => { groupedContacts[key].sort((a, b) => a.name.localeCompare(b.name)); }); ``` 4. 在页面上渲染每个分组及其对应的联系人: ```html <template> <div> <div v-for="(contacts, letter) in groupedContacts" :key="letter"> <h2>{{ letter }}</h2> <ul> <li v-for="contact in contacts" :key="contact.phone">{{ contact.name }}</li> </ul> </div> </div> </template> <script> export default { data() { return { groupedContacts: {}, }; }, mounted() { // 获取联系人数据并处理分组排序 // ... this.groupedContacts = groupedContacts; }, }; </script> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值