引入pinyin 插件
import pinyin from "../../node_modules/js-pinyin/index"
接口调用处理
post({
url: "/api/list"
}).then(({ data }) => {
let segs = []
let res = {}
let curr
let list = data.data.list.map(item => {
return {
pinyin: pinyin.getCamelChars(item.name_zh).substr(0, 1),
...item
}
})
let letters = "*ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("")
letters.filter((items, i) => {
curr = {
initials: "", //字母
data: [] //数据
}
list.map(v => {
if (v.pinyin[0].toUpperCase() == items) {
curr.data.push(v)
}
})
if (curr.data.length) {
curr.initials = letters[i]
segs.push(curr)
curr.data.sort((a, b) => {
return a.pinyin.localeCompare(b.pinyin)
})
}
})
res.segs = Array.from(new Set(segs))
// this.getChooseList= this.getChooseList.data
res.segs.forEach(function(val){
val.data.forEach(function(v){
v.checkeds = false
})
})
console.log(res.segs)
})