我想获得如下的JSON:
myJson = {"QC": ["city1","city2"], "NU": ["city3", "city4"], "FO": ["city5","city6"]};
我从数据库中获取数据,结构如下:
id province city
1 QC city1
2 NU city3
3 FO city5
4 NU city4
5 QC city2
6 FO city6
坦克非常适合tipps
答案 0 :(得分:1)
循环遍历行。保持province
为关键。在添加之前,请检查密钥是否已存在。如果已经存在则添加到array.Something like this,
if(obj[key]){
obj[key].push(city);
}else{
obj[key] = [];
obj[key].push(city);
}