GithubHelp home page GithubHelp logo

bg7zag / topinyin Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 3.0 31 KB

uniapp 文字汉字一键转拼音,转简写,转首字母 插件改进版 原作者: https://ext.dcloud.net.cn/plugin?id=3294

License: Other

JavaScript 100.00%

topinyin's Introduction

toPinyin

uniapp 文字汉字一键转拼音,转简写,转首字母

封装了下使用方法,原作者: [email protected] https://ext.dcloud.net.cn/plugin?id=3294

1、引入js

import toPinyin from "./toPinyin"

2、转拼音

toPinyin.chineseToPinYin('你好')

输出

NiHao

3、转首字母

toPinyin.chineseToInitials(toPinyin.chineseToPinYin('你好'))

或者

toPinyin.chineseToInitials('NiHao')

输出

NH

提供个一位数组转成按照首字母排序的方法(在 utils.js 文件中):

import toPinyin from "./toPinyin";

/**
 * 根据拼音首字母筛选排序分组
 * @param {Array} arr 原数组
 * @param {String} key 原数组需要筛选的字段
 * @returns {Array} 返回一个[{name: A,value: []}] 格式的二维数组
 */
export function getGroupByPinyin(arr, key = 'name') {
    if(!arr) return
    
    // 获取A-Z字母数组
    let keys = [...Array(26).keys()].map((i) => String.fromCharCode(i + 65));
    
    arr = arr.map((n) => ({
        ...n,
        py: toPinyin.chineseToInitials(
            toPinyin.chineseToPinYin(n[key].substr(0, 1))
        ),
    }));

    let group = [];
    for (const i of keys) {
        // 新数组一级结构,可自行修改
        let item = {
            name: i,
            value: [],
        };
        for (const j of arr) {
            if (j.py === i) {
                item.value.push(j);
            }
        }
        if (item.value.length > 0) {
            item.value.sort((a, b) => a[key].localeCompare(b[key]));
            group.push(item);
        }
    }
    return group;
}

topinyin's People

Contributors

bg7zag avatar

Stargazers

 avatar  avatar Xu-haofeng avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.