JS實(shí)現(xiàn)拼音(字母)匹配漢字(姓名)的示例代碼
這就是個(gè)模糊查詢,我們平常做的都是直接輸入漢字去把對(duì)應(yīng)的值過濾出來,但我還真是第一次通過拼音去查詢(當(dāng)然不只是拼音,漢字也是可以的),以前還真沒注意這個(gè)。唉,這可咋搞,我怎么知道某個(gè)漢字(字符串)的拼音的首字母是什么呢?正當(dāng)我愁眉苦臉的時(shí)候,哎,一個(gè)庫被我發(fā)現(xiàn)了,hhhhh~。就是pinyin-match。下面介紹該包的使用,這個(gè)包也不大,四百多kb。蠻高效的。下面介紹一下該庫的使用
npm官方:https://www.npmjs.com/package/pinyin-match
1. 首先說下功能
它能夠使用拼音快速檢索目標(biāo)。當(dāng)然也可以使用漢字
- 簡體版27KB (gzip ≈ 19KB),繁體版86KB (gzip ≈ 60KB)
- 支持多音字、繁體字、拼音首字母匹配,具備分詞功能
- 返回位置信息,可用于高亮匹配字符
- 在長多音字串下依然有高性能
在線演示:http://laosep.top/pinyin-match/
2. 安裝
npm install pinyin-match --save
3. 引入
簡體版:
import PinyinMatch from 'pinyin-match'; // es
const PinyinMatch = require('pinyin-match'); // commonjs
繁體版:
import PinyinMatch from 'pinyin-match/es/traditional.js'; // es
const PinyinMatch = require('pinyin-match/lib/traditional.js'); // commonjs
當(dāng)然也可以script引入
// 簡體: <script src="pinyin-match/dist/main.js"></script> // 繁體: <script src="pinyin-match/dist/traditional.js"></script>
4. API
.match(input, keyword) //查詢匹配拼音的數(shù)據(jù)。
只向外提供暴露了這么一個(gè)方法,這一個(gè)方法足夠我們使用了,也很方便
參數(shù):
- input {string} 目標(biāo)字符串
- keyword {string} 輸入的拼音或其他關(guān)鍵詞
返回:
Array | Boolen
5. 簡單使用測試示例
let test = '123曾經(jīng)滄海難為水除卻巫山不是云'
PinyinMatch.match(test, '23曾'); // [1, 3]
PinyinMatch.match(test, 'cjc') // [3, 5]
PinyinMatch.match(test, 'cengjingcanghai') // [3, 6]
PinyinMatch.match(test, 'cengjingcangha') // [3, 6]
PinyinMatch.match(test, 'engjingcanghai') // false
PinyinMatch.match(test, 'zengjingcang') // [3, 5]
PinyinMatch.match(test, 'sdjkelwqf') // false
PinyinMatch.match(test, 'zengji ng cang') // [3, 5]
PinyinMatch.match(test, 'zengji ng cangsdjfkl') // false
PinyinMatch.match(' 我 愛你 中 國 ', 'nzg') // [6, 12]
PinyinMatch.match(' 我 愛你 中 國 ', '愛你中') // [5, 8]
PinyinMatch.match('發(fā)', 'fa') // [0, 0]6. 具體案例
就是平常的列表展示加模糊搜索。所用的人員列表測試數(shù)據(jù)都是下面這種格式,章末會(huì)把完整測試數(shù)據(jù)附上。
// 模擬后端返回的數(shù)據(jù)
export default [
{
name: '管理員',
no: 'FT00000'
},
//...
]其實(shí)很簡單的了,主要是實(shí)現(xiàn)拼音的搜索過濾的功能,所以我案例里面樣式也沒調(diào),主要是功能
閑話不說了,直接上完整代碼,大家看下里面的邏輯都明白了,就這么一個(gè)組件:
<template>
<div class="main">
<input type="text" v-model="serchValue" placeholder="輸入搜索">
<div class="user" v-for="(user, index) in users" :key="user.no">{{ index }}# : {{ user.name }}</div>
</div>
</template>
<script>
import userList from './data/user'
import PinyinMatch from 'pinyin-match'
let timer = null
export default {
data() {
return {
serchValue: '',
userListAll: [], // 所有數(shù)據(jù)
users: [] // 展示的數(shù)據(jù)
}
},
watch: {
serchValue() {
this.debounce(this.selectUser, 200)
}
},
mounted(){
this.getUserList()
},
methods:{
// 模擬請(qǐng)求
getUserList() {
setTimeout(() => {
this.userListAll = userList
this.selectUser()
}, 100)
},
// 防抖
debounce(fn, wait) {
if(timer) clearTimeout(timer)
timer = setTimeout(() => {
fn.call(this)
timer = null
}, wait)
},
// 模糊查詢條件
selectUser() {
this.users = []
// 如果搜索框有值的話再去過濾,因?yàn)镻inyinMatch.match第二個(gè)參數(shù)是空字符串的話會(huì)匹配不到,返回false,這不符合我們的預(yù)期
// 搜索框沒有值,我們要展示所有數(shù)據(jù)
if(this.serchValue) {
this.userListAll.forEach(user => {
let matchIndexs = PinyinMatch.match(user.name, this.serchValue) // 如果匹配到返回 首尾的索引數(shù)組,如果匹配不到則返回false
if(matchIndexs) {
this.users.push(user)
}
})
} else {
this.users = this.userListAll
}
}
}
}
</script>
<style scoped>
.main {
width: 100vw;
height: 100vh;
padding: 200px 0 0 200px;
box-sizing: border-box;
}
.main input {
margin-bottom: 5px;
}
</style>接下來看下效果:

接下來!我們可以把上面的示例再提高點(diǎn)兒難度,因?yàn)樽屛蚁肫鹆送ㄓ嶄?,所以在展示的時(shí)候我們就像通訊錄那樣展示
首先將拿到的人員列表進(jìn)行分組,根據(jù)什么分呢?就是根據(jù)字母,a、b、c...這樣,難道我們要把26個(gè)英文字母全都列出來嗎?當(dāng)然這個(gè)也分需求,但如果是通訊錄的話,我們只需要百家姓中所有的拼音的首字母就可以了,也就是:abcdefghjklmnopqrstwxyz 這些。
在上面示例的基礎(chǔ)上進(jìn)行修改,在拿到人員數(shù)據(jù)之后,先處理一下,然后再進(jìn)行過濾模糊查詢
完整代碼:
<template>
<div class="main">
<input type="text" v-model="serchValue" placeholder="輸入搜索">
<div class="users" v-for="user in users" :key="user.key">
<div>{{ user.key }}</div>
<div class="user-name" v-for="o in user.data" :key="o.no">{{ o.name }}</div>
</div>
</div>
</template>
<script>
import userList from './data/user'
import PinyinMatch from 'pinyin-match'
let timer = null
export default {
data() {
return {
serchValue: '',
userListAll: [], // 所有數(shù)據(jù)
users: [] // 展示的數(shù)據(jù)
}
},
watch: {
serchValue() {
this.debounce(this.selectUser, 200)
}
},
mounted(){
this.getUserList()
},
methods:{
// 模擬請(qǐng)求
getUserList() {
setTimeout(() => {
this.userListAll = this.handlerData(userList)
this.selectUser()
}, 100)
},
// 處理數(shù)據(jù)
handlerData(userList) {
// 這是百家姓中所有的拼音的首字母
const surnameLetters = 'abcdefghjklmnopqrstwxyz'.split('')
const userListAll = []
surnameLetters.forEach(letter => {
let o = { key: letter, data: [] }
userList.forEach(user => {
let matchIndexs = PinyinMatch.match(user.name.slice(0, 1), letter) // 匹配姓氏的拼音的首字母
if(matchIndexs) {
o.data.push(user)
}
})
if(o.data.length) {
userListAll.push(o)
}
})
return userListAll
},
// 防抖
debounce(fn, wait) {
if(timer) clearTimeout(timer)
timer = setTimeout(() => {
fn.call(this)
timer = null
}, wait)
},
// 模糊查詢條件
selectUser() {
this.users = []
if(this.serchValue) {
this.userListAll.forEach(user => {
let o = { key: user.key, data: [] }
user.data.forEach(item => {
let matchIndexs = PinyinMatch.match(item.name, this.serchValue)
if(matchIndexs) {
o.data.push(item)
}
})
if(o.data.length) {
this.users.push(o)
}
})
} else {
this.users = this.userListAll
}
}
}
}
</script>
<style scoped>
.main {
width: 100%;
height: 100%;
padding: 0 0 0 200px;
box-sizing: border-box;
}
.main input {
margin-bottom: 5px;
}
.user-name {
padding-left: 10px;
}
</style>最后看下修改后的效果:

接下來是上面測試用的數(shù)據(jù)
export default [
{
"name": "管理員",
"no": "FT00000"
},
{
"name": "朱大錘",
"no": "FT00001"
},
{
"name": "郝大錘",
"no": "FT00002"
},
{
"name": "宋大錘",
"no": "FT00003"
},
{
"name": "楊大錘",
"no": "FT00004"
},
{
"name": "石大錘",
"no": "FT00005"
},
{
"name": "鄭大錘",
"no": "FT00006"
},
{
"name": "劉大錘",
"no": "FT00007"
},
{
"name": "趙大錘",
"no": "FT00008"
},
{
"name": "李大錘",
"no": "FT00009"
},
{
"name": "牛二",
"no": "FT00010"
},
{
"name": "張大錘",
"no": "FT00011"
},
{
"name": "王大錘",
"no": "FT00012"
},
{
"name": "馮大錘",
"no": "FT00013"
},
{
"name": "李大錘",
"no": "FT00014"
},
{
"name": "鄧大錘",
"no": "FT00015"
},
{
"name": "孫大錘",
"no": "FT00016"
},
{
"name": "袁大錘",
"no": "FT00017"
},
{
"name": "康大錘",
"no": "FT00018"
},
{
"name": "武大錘",
"no": "FT00019"
},
{
"name": "蔡大錘",
"no": "FT00020"
},
{
"name": "戴大錘",
"no": "FT00021"
},
{
"name": "鄂大錘",
"no": "FT00022"
},
{
"name": "封大錘",
"no": "FT00023"
},
{
"name": "蓋大錘",
"no": "FT00024"
},
{
"name": "景大錘",
"no": "FT00025"
},
{
"name": "麻大錘",
"no": "FT00026"
},
{
"name": "那大錘",
"no": "FT00027"
}
]到此這篇關(guān)于JS實(shí)現(xiàn)拼音(字母)匹配漢字(姓名)的示例代碼的文章就介紹到這了,更多相關(guān)JS拼音匹配漢字內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
FireFox JavaScript全局Event對(duì)象
在IE下 JavaScript 中可以在任何地方使用全局的window.event來取得本次JavaScript被觸發(fā)的Event,從而取得 KeyCode,EventSourceElement 等對(duì)象。2009-06-06
Js實(shí)現(xiàn)雙擊鼠標(biāo)自動(dòng)滾動(dòng)屏幕的示例代碼
這篇文章主要介紹了Js實(shí)現(xiàn)雙擊鼠標(biāo)自動(dòng)滾動(dòng)屏幕的示例代碼。需要的朋友可以過來參考下,希望對(duì)大家有所幫助2013-12-12
JavaScript & jQuery完美判斷圖片是否加載完畢
本文主要介紹了JavaScript & jQuery完美判斷圖片是否加載完畢的方法。具有一定的參考價(jià)值,下面跟著小編一起來看下吧2017-01-01
swiper 自動(dòng)圖片無限輪播實(shí)現(xiàn)代碼
今天移動(dòng)端正好需要圖片觸摸滑動(dòng)效果實(shí)現(xiàn)代碼,基于swiper實(shí)現(xiàn),需要的朋友可以參考下2018-05-05
javascript 小數(shù)取整簡單實(shí)現(xiàn)方式
這篇文章主要介紹了javascript 小數(shù)取整d的簡單實(shí)現(xiàn)方式,需要的朋友可以參考下2014-05-05
js仿支付寶填寫支付密碼效果實(shí)現(xiàn)多方框輸入密碼
這篇文章主要介紹了js仿支付寶填寫支付密碼效果實(shí)現(xiàn)多方框輸入密碼的功能,感興趣的小伙伴們可以參考一下2016-03-03
JavaScript實(shí)現(xiàn)事件總線(Event?Bus)的方法詳解
Event?Bus?事件總線,通常作為多個(gè)模塊間的通信機(jī)制,相當(dāng)于一個(gè)事件管理中心。本文將介紹如何在JavaScript中實(shí)現(xiàn)事件總線,需要的可以參考一下2022-05-05

