注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

love3400wind的blog

like

 
 
 

日志

 
 
 
 

javascript 将(平行)数组转化为树  

2015-02-28 15:38:31|  分类: IT |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
原始数据:

[
{"id":224,"fid":0,"name":"商品类"},
{"id":225,"fid":0,"name":"生活服务类"},
{"id":226,"fid":0,"name":"餐饮类"},
{"id":227,"fid":224,"name":"食品"},
{"id":228,"fid":224,"name":"化妆品"},
{"id":229,"fid":224,"name":"服装"},
{"id":230,"fid":224,"name":"鞋包"},
{"id":231,"fid":224,"name":"家居"},
{"id":232,"fid":224,"name":"家电"},
{"id":233,"fid":224,"name":"IT产品"},
{"id":234,"fid":225,"name":"生活服务"},
{"id":235,"fid":225,"name":"休闲娱乐"},
{"id":236,"fid":225,"name":"美容保健"},
{"id":248,"fid":226,"name":"地方菜系"},
{"id":249,"fid":226,"name":"自助海鲜"},
{"id":250,"fid":226,"name":"蛋糕甜品"},
{"id":252,"fid":226,"name":"日式韩系"},
{"id":253,"fid":226,"name":"西餐国际"},
{"id":254,"fid":226,"name":"火锅烧烤"},
{"id":255,"fid":226,"name":"其他餐饮"},
{"id":256,"fid":227,"name":"辣条"},
{"id":257,"fid":227,"name":"酒鬼花生"},
{"id":258,"fid":227,"name":"三文鱼"},
{"id":260,"fid":228,"name":"欧莱雅"},
{"id":261,"fid":229,"name":"KAPPA"},
{"id":237,"fid":234,"name":"汽车养护"},
{"id":238,"fid":234,"name":"上门洗车"},
{"id":239,"fid":234,"name":"宠物护理"},
{"id":240,"fid":234,"name":"洗衣洗护"},
{"id":241,"fid":234,"name":"教育培训"},
{"id":242,"fid":234,"name":"厨师到家"},
{"id":243,"fid":235,"name":"游乐游艺"},
{"id":244,"fid":235,"name":"运动健身"},
{"id":245,"fid":236,"name":"美体瘦身"},
{"id":246,"fid":236,"name":"养生按摩"},
{"id":247,"fid":236,"name":"美容美发"},
{"id":259,"fid":243,"name":"广场舞"}
]

处理后的数据:

[
{"fid":"0","id":"224","name":"商品类","children":
[
{"fid":"224","id":"227","name":"食品","children":
[
{"fid":"227","id":"256","name":"辣条"},
{"fid":"227","id":"257","name":"酒鬼花生"},
{"fid":"227","id":"258","name":"三文鱼"}
]
},
{"fid":"224","id":"228","name":"化妆品","children":
[
{"fid":"228","id":"269","name":"面膜"},
{"fid":"228","id":"270","name":"眼霜"}
]
},
{"fid":"224","id":"229","name":"服装","children":
[
{"fid":"229","id":"262","name":"男装","children":
[
{"fid":"262","id":"265","name":"卡帝乐"},
{"fid":"262","id":"266","name":"老人头"}
]
},
{"fid":"229","id":"263","name":"女装","children":
[
{"fid":"263","id":"267","name":"vero mode"}
]
},
{"fid":"229","id":"264","name":"童装","children":
[
{"fid":"264","id":"268","name":"巴拉拉"}
]
}
]
},
{"fid":"224","id":"230","name":"鞋包","children":
[
{"fid":"230","id":"271","name":"男鞋"},
{"fid":"230","id":"272","name":"女鞋"}
]
},
{"fid":"224","id":"231","name":"家居","children":
[
{"fid":"231","id":"273","name":"床垫"},
{"fid":"231","id":"274","name":"枕头"}
]
},
{"fid":"224","id":"232","name":"家电","children":
[
{"fid":"232","id":"275","name":"大家电"},
{"fid":"232","id":"276","name":"小家电"}
]
},
{"fid":"224","id":"233","name":"IT产品","children":
[
{"fid":"233","id":"277","name":"手机"},
{"fid":"233","id":"278","name":"相机"}
]
}
]
},
{"fid":"0","id":"225","name":"生活服务类","children":
[
{"fid":"225","id":"234","name":"生活服务","children":
[
{"fid":"234","id":"237","name":"汽车养护"},
{"fid":"234","id":"238","name":"上门洗车"},
{"fid":"234","id":"239","name":"宠物护理"},
{"fid":"234","id":"240","name":"洗衣洗护"},
{"fid":"234","id":"241","name":"教育培训"},
{"fid":"234","id":"242","name":"厨师到家"}
]
},
{"fid":"225","id":"235","name":"休闲娱乐","children":
[
{"fid":"235","id":"243","name":"游乐游艺","children":
[
{"fid":"243","id":"259","name":"广场舞"}
]
},
{"fid":"235","id":"244","name":"运动健身"}
]
},
{"fid":"225","id":"236","name":"美容保健","children":
[
{"fid":"236","id":"245","name":"美体瘦身"},
{"fid":"236","id":"246","name":"养生按摩"},
{"fid":"236","id":"247","name":"美容美发"}
]
}
]
},
{"fid":"0","id":"226","name":"餐饮类","children":
[
{"fid":"226","id":"248","name":"地方菜系"},
{"fid":"226","id":"249","name":"自助海鲜"},
{"fid":"226","id":"250","name":"蛋糕甜品"},
{"fid":"226","id":"252","name":"日式韩系"},
{"fid":"226","id":"253","name":"西餐国际"},
{"fid":"226","id":"254","name":"火锅烧烤"},
{"fid":"226","id":"255","name":"其他餐饮"}
]
}
]


js函数:
/**
* 将json格式的数据转换成树形结构
* @param json 平行的数组,上级必须在前面
* @param subKeyName 添加的子项对应的key名 默认为: children
* @param childKey 子元素对应关系的key名 默认为: id
* @param parentKey 父元素对应关系的key名 默认为: fid
*/
$scope.toTree = function (json) {

var l=json.length;
var arr = [];

/**
* 检测数组中是否有元素是给定元素的父亲
* @param data 给定元素
* @param arr 给定数组
* @returns {number} 在数组中找给定元素的母亲,如果找到返回下标,若未找到返回-1
*/
var finding = function (data, arr) {
var index = -1;
for(var k in arr){
if (arr[k].id == data.fid) {
index = k;
break;
}
}
return index;
}

/**
* 递归处理:将元素插入到数组中
* @param data
* @param arr
*/
var recursivePushing = function (data, arr) {
var index = finding(data, arr);
if (index == -1) { //没找到元素
for (var kk in arr) {
recursivePushing(data, arr[kk].children);
}
} else {//找到元素
if (!arr[index].children) {
arr[index].children = [];
}
arr[index].children.push(data);
}
}

for(var i = 0; i < l; i++){

(function (data) {
//var jsonArray = data;
if (data.fid == 0) {
arr.push(data);
} else {
recursivePushing(data, arr);
}
})(json[i]);
}

console.log(JSON.stringify(arr));
}

  评论这张
 
阅读(146)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017