var data = [{
"name": "涡阳",
"pinyin": "WoYang"
}, {
"name": "邳州",
"pinyin": "PiZhou"
}, {
"name": "玉溪",
"pinyin": "YuXi"
}, {
"name": "无锡",
"pinyin": "WuXi"
}, {
"name": "萍乡",
"pinyin": "PingXiang"
}];
Kategori mengikut huruf A B C...senarai
seperti
P->
{
"name": "萍乡",
"pinyin": "PingXiang"
},
{
"name": "邳州",
"pinyin": "PiZhou"
}
Huraian pautan yang serupa
Cara mengendalikan set data ini. .
我的代码,比较渣 求更优雅的写法
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
}