当前位置: 首页 > Web前端 > HTML

中文排序,分块

时间:2023-03-28 10:57:11 HTML

有个类似微信通讯录的功能,但是和安卓有兼容性问题,不过也记录一下吧。//arr是传入的数组groupByDesc(arr){const[...zhCN]='啊,八摩摩和蛾毛喀哈击喀拉妈妈哦帕齐然萨塔挖喜按赞'//const[...EN]='abcdefghijklmnopqrstuvwxyz'const[...EN]='ABCDEFGHIJKLMNOPQRSTUVWXYZ'//i,u,v下的中文没有对应的汉字//const[...desc]='abcdefghjklmnopqrstwxyz'const[...desc]='ABCDEFGHJKLMNOPQRSTWXYZ'//中文constisChinese=/[\u4e00-\u9fa5]+///英文constpattern=/[a-zA-Z]/;//constisSpecial=/[0-9*#@!%$&(),.+-]+/consthashMap={}functionfindIndex(obj){letcheckvarlengthlettype='EN'if(isChinese.test(obj)){length=zhCN.lengthcheck=zhCNtype='zhCN'console.log('Chinese')}else{length=EN.lengthcheck=ENconsole.log('English')}for(vari=0;i{lethashCode//判断不正确ChineseJapaneseEnglishif(!pattern.test(item.nickname[0])&&!isChinese.test(item.nickname[0])){hashCode='#'//中文}elseif(isChinese.test(item.nickname[0])){hashCode=desc[findIndex(item.nickname[0])]//英文}else{hashCode=EN[findIndex(item.nickname[0])]}//空if(!hashMap[hashCode]){hashMap[hashCode]=[]}//valuehashMap[hashCode].push(item)})console.log(hashMap)返回hashMap}