• 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','#']

  • 相关阅读:
    【C++】string的使用
    DIV-CSS布局
    【真题笔记】14年系统架构设计师要点总结
    广义线性模型(GLM)及其应用
    Flink部署——Metric Reporters
    Linux服务器报错“No space left on device”如何解决
    安卓面试总结(1)——Java基础
    【# 软件stm32cubeIDE下使用STM32F103的ADC+DMA测量-基础样例+进阶+实例应用>>热敏电阻温度测量】
    聊聊arthas的spring-boot-starter
    一文读懂Vue.js与React.js的区别
  • 原文地址:https://blog.csdn.net/weixin_44523517/article/details/132845129