zoukankan      html  css  js  c++  java
  • js-pinyin包实现城市按首字母排序

    后台从接口请求回来的城市相关的数据只有城市名称,没有排序,铺页面的时候要排序就很麻烦;

    面向百度编程时候找到了一个包,用它来将字符串转成拼音,就可以通过字符串截取取出拼音首字母,这样就可以进行首字母排序了。

    这个包的名字叫js-pinyin

    npm i js-pinyin 
    //安装拼音库
    import pyjs from 'js-pinyin'
    //引入依赖包 pyjs.getFullChars(item.name)
    //获取字符串全部拼音,并且首字母大写; toLocaleLowerCase().slice(0, 1)
    //全部转化成小写,并取首字母
    • getFullChars():获取字符串全部拼音,并且首字母大写;
    • getCamelChars() : 获取字符串拼音首字母,并大写;
     

    案例

        pinyin.setOptions({checkPolyphone: false, charCase: 0});
        console.log(pinyin.getFullChars('管理员'));    //GuanLiYuan
        console.log(pinyin.getCamelChars('管理员'));    //GLY
        console.log(pinyin.getCamelChars('1234'));    //1234
        console.log(pinyin.getCamelChars('english'));    //english
  • 相关阅读:
    POJ 2209
    POJ 2196
    POJ 2215
    POJ 2192
    POJ 2195
    POJ 2181
    POJ 2182
    POJ 2159
    POJ 2153
    字符设备驱动 —— 字符设备驱动框架
  • 原文地址:https://www.cnblogs.com/suni1024/p/12156428.html
Copyright © 2011-2022 走看看