• Vue 汉字转拼音;根据拼音首字母排序转二维数组;提取拼音首字母排序。


    一、基本使用

    下载依赖

    cnpm i js-pinyin

    使用:

    import Pinyin from 'js-pinyin';
    1. console.log(Pinyin.getFullChars('管理员')); // GuanLiYuan
    2. console.log(Pinyin.getCamelChars('管理员')); // GLY;

    二、根据拼音首字母排序转二维数组

    原数组:

    1. const newIndexList = [
    2. { username: '张三' },
    3. { username: '张四' },
    4. { username: '李四' },
    5. { username: '王五' }
    6. [

    根据拼音首字母排序转二维数组

    1. // 创建一个空对象,用于存储分组
    2. const grouped = {};
    3. // 遍历原始数组并进行分组
    4. newIndexList.forEach(item => {
    5. const username = item.username;
    6. const pinyin = Pinyin.getFullChars(username);
    7. const firstLetter = pinyin.charAt(0).toUpperCase();
    8. if (!grouped[firstLetter]) {
    9. grouped[firstLetter] = [];
    10. }
    11. grouped[firstLetter].push(item);
    12. });
    13. // 将分组后的对象转换为数组
    14. // 获取分组后的键(首字母),并排序
    15. const groupedKeys = Object.keys(grouped).sort();
    16. // 创建一个按首字母排序的结果数组
    17. const sortedGroupedArray = groupedKeys.map(key => grouped[key]);
    18. return sortedGroupedArray
    19. /*
    20. [
    21. [
    22. {username: '王五'},
    23. ],
    24. [
    25. {username: '李四'},
    26. ],
    27. [
    28. {username: '张三'},
    29. {username: '张四'},
    30. ]
    31. ]
    32. */

    如果需要非汉字或英文的分到 # 组中:
    正则表达式 /^[a-zA-Z\u4e00-\u9fa5]/ 来判断 username 头开是否为汉字或英文。如果是,则使用 Pinyin.getFullChars(username).charAt(0).toUpperCase() 获取首字母;否则,将其分到 # 组中

    1. // 创建一个空对象,用于存储分组
    2. const grouped = {};
    3. // 遍历原始数组并进行分组
    4. newIndexList.forEach(item => {
    5. const username = item.username;
    6. const isChineseOrEnglish = /^[a-zA-Z\u4e00-\u9fa5]/.test(username); // 判断是否为汉字或英文
    7. const firstLetter = isChineseOrEnglish ?
    8. Pinyin.getFullChars(username).charAt(0).toUpperCase() : '#';
    9. if (!grouped[firstLetter]) {
    10. grouped[firstLetter] = [];
    11. }
    12. grouped[firstLetter].push(item);
    13. });
    14. // 将分组后的对象转换为数组
    15. // 获取分组后的键(首字母),并排序
    16. const groupedKeys = Object.keys(grouped).sort();
    17. // 将不为汉字或英文的元素放在同一组 # 中,并确保 # 组放在最后
    18. if (groupedKeys.includes('#')) {
    19. groupedKeys.splice(groupedKeys.indexOf('#'), 1);
    20. groupedKeys.push('#');
    21. }
    22. // 创建一个按首字母排序的结果数组
    23. const sortedGroupedArray = groupedKeys.map(key => grouped[key]);
    24. return sortedGroupedArray
    25. /*
    26. [
    27. [
    28. {username: '王五'},
    29. ],
    30. [
    31. {username: '李四'},
    32. ],
    33. [
    34. {username: '张三'},
    35. {username: '张四'},
    36. ]
    37. ]
    38. */

    三、提取拼音首字母排序:

    1. // 创建一个空数组,用于存储拼音首字母
    2. const pinyinFirstLetters = [];
    3. // 遍历原始数组并获取拼音首字母
    4. newIndexList.forEach(item => {
    5. const username = item.username;
    6. const pinyin = Pinyin.getFullChars(username);
    7. const firstLetter = pinyin.charAt(0).toUpperCase();
    8. if (!pinyinFirstLetters.includes(firstLetter)) {
    9. pinyinFirstLetters.push(firstLetter);
    10. }
    11. });
    12. // 过滤出英文字母,然后在最后加一个 '#'
    13. let lettersArray = pinyinFirstLetters.sort().filter(item => /[a-zA-Z]/.test(item));
    14. lettersArray.push('#')
    15. return lettersArray // ['W','L','Z','#']

  • 相关阅读:
    【智能优化算法】基于阴阳对优化算法求解单目标优化问题附matlab代码 Yin Yang Pair Optimization
    python+pytest接口自动化(12)-自动化用例编写思路 (使用pytest编写一个测试脚本)
    String常见面试题
    夜莺日志采集mtail
    Android Qcom Display学习(十一)
    基于分布鲁棒优化的电-气-热综合能源系统日前经经济调度
    限制IP到全流程防控,讲解网络爬虫与技术反爬的动态攻防
    Vue学习:el 与data的两种写法
    贪心算法小结
    火车头双标题插件-火车头采集器双标题插件下载及安装教程
  • 原文地址:https://blog.csdn.net/weixin_44523517/article/details/132845129