var data = [{
"name": "涡阳",
"pinyin": "WoYang"
}, {
"name": "邳州",
"pinyin": "PiZhou"
}, {
"name": "玉溪",
"pinyin": "YuXi"
}, {
"name": "无锡",
"pinyin": "WuXi"
}, {
"name": "萍乡",
"pinyin": "PingXiang"
}];
Sort by letters A B C... list
Such as
P->
{
"name": "萍乡",
"pinyin": "PingXiang"
},
{
"name": "邳州",
"pinyin": "PiZhou"
}
Similar link description
How to operate this set of data. .
我的代码,比较渣 求更优雅的写法
function jsfu(data) {
var obj={}
data.map(function(item, index) {
let codeIndex = item.pinyin.charAt(0)
if (!obj[codeIndex]) {
obj[codeIndex] = {
item: []
}
}
obj[codeIndex].item.push(item)
})
return obj
}