jquery each遍历json插入数组的问题
接收到json:{"list":[{"id":3010,"name":"青海","cityList":[{"id":3011,"name":"西宁"},{"id":301...
接收到json:
{"list":[{"id":3010,"name":"青海","cityList":[{"id":3011,"name":"西宁"},{"id":3012,"name":"城东区"},{"id":3013,"name":"城西区"}]},
我想以下面这张格式push进数组:
var cityData = [{value: '110000',text: '北京市',children: [{value: "110101",text: "东城区"}, {value: "110102",text: "西城区"}]}]
我自己写的代码:
success: function(data) {
var arr = [];
jQuery.each(data.list, function(i, item) {
arr.push({value: data.list[i].id,text: data.list[i].name})
jQuery.each(item,function(k,item1){
//arr.push()
})
})
cityPicker.setData(arr);
}
我要怎么改才能达到我想要的效果啊! 展开
{"list":[{"id":3010,"name":"青海","cityList":[{"id":3011,"name":"西宁"},{"id":3012,"name":"城东区"},{"id":3013,"name":"城西区"}]},
我想以下面这张格式push进数组:
var cityData = [{value: '110000',text: '北京市',children: [{value: "110101",text: "东城区"}, {value: "110102",text: "西城区"}]}]
我自己写的代码:
success: function(data) {
var arr = [];
jQuery.each(data.list, function(i, item) {
arr.push({value: data.list[i].id,text: data.list[i].name})
jQuery.each(item,function(k,item1){
//arr.push()
})
})
cityPicker.setData(arr);
}
我要怎么改才能达到我想要的效果啊! 展开
展开全部
// by sleest 2016/05/10
var res='{"list":[{"id":3010,"name":"青海","cityList":[{"id":3011,"name":"西宁"},{"id":3012,"name":"城东区"},{"id":3013,"name":"城西区"}]}]}';
var dataObj = JSON.parse(res);
var listData = dataObj.list;
var cityAry = [];
for(var i=0;i<listData.length;i++){
var tmp = listData[i];
var city = {
value:tmp.id,
text:tmp.name,
children:[]
}
for(var j=0;j<tmp.cityList.length;j++){
city.children.push({
value:tmp.cityList[j].id,
text:tmp.cityList[j].name
})
}
cityAry.push(city);
}
console.table(cityAry);
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询