一、基本使用
下载依赖
cnpm i js-pinyin
使用:
import Pinyin from 'js-pinyin';
console.log(Pinyin.getFullChars('管理员')); // GuanLiYuan
console.log(Pinyin.getCamelChars('管理员')); // GLY;
二、根据拼音首字母排序转二维数组
原数组:
const newIndexList = [
{ username: '张三' },
{ username: '张四' },
{ username: '李四' },
{ username: '王五' }
[
根据拼音首字母排序转二维数组:
// 创建一个空对象,用于存储分组
const grouped = {};
// 遍历原始数组并进行分组
newIndexList.forEach(item => {
const username = item.username;
const pinyin = Pinyin.getFullChars(username);
const firstLetter = pinyin.charAt(0).toUpperCase();
if (!grouped[firstLetter]) {
grouped[firstLett