[
dictprovinceVOList[{
areaId: 13257
cityAreaList:[
{
areaCode: "1853"
areaId: 13265
areaList:
firstLetter:"S"
langType:"zh_CN"
latitude:22.124049
longitude:
113.559954
parentId:13257
pinYin:"shengfangjigetangqu"
postCode:"820008"
sortValue:8
title:"圣方济各堂区"
},
{
...
}]
parentId: 0
sortValue: 34
title:"澳门特别行政区"
},{
areaId:13238
cityAreaList:[
{
areaCode: "1853"
areaId: 13265
areaList:
firstLetter:"S"
langType:"zh_CN"
latitude:22.124049
longitude:
113.559954
parentId:13257
pinYin:"shengfangjigetangqu"
postCode:"820008"
sortValue:8
title:"圣方济各堂区"
},{
...
}]
parentId:0
sortValue:33
title:"香港特别行政区"
}]
]
{
"iniData":"S",
"cityAreaList":[
{
"areaCode":"1853","areaId": 372, ...
},
{...}
]
}
PHP中文网2017-05-19 10:47:12
lodash应该没有一步到位的函数
但有个groupBy函数可以用来分组
你可以用一个循环把所有城市的cityAreaList合到一个array
let array = [];
for(let city of citylist){
array = array.contact(city.cityAreaList)
}
然后
_.groupBy(array,function(obj){
return obj["firstLetter"];
})
返回是一个object是大概是这种结构的
{
s:[
{
"areaCode":"1853","areaId": 372, ...},
{...}
]
p:{
...
}
}
这个object的key就是你想要的"iniData"
剩下的事情就简单了
用Object.keys()获取object的所以key
let keys = Object.keys(object);
let newArr = [];
for(let key of keys){
newArr.push(
{
"iniData":key,
"cityAreaList":object[key]
}
)
}
这个newArr就是你想要的结果