[
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就是你想要的結果