今天小颖在工作中遇到要遍历树得问题了,实现后,怕后期遇到又忘记啦,所以记录下嘻嘻,其实这个和小颖之前写过得一篇文章 json的那些事 中第4点有关json的面试题有些类似。
数组格式:
var arry = [{
"id": "11",
"name": "huanhuan",
"pid": "0",
"isleaf": 2,
"child": [{
"name": "豆豆",
"pid": "22",
"id": "222",
"isleaf": 1
},
{
"name": "huanhuanhuan",
"pid": "2323",
"id": "2324",
"isleaf": 1
}
]
},
{
"id": "1212",
"name": "大大",
"pid": "0",
"isleaf": 2,
"child": [{
"name": "1234",
"pid": "1313",
"id": "4444",
"isleaf": 1
}]
},
{
"id": "666",
"name": "光头强爱上熊二",
"pid": "0",
"isleaf": 1,
"child": null
},
{
"id": "888",
"name": "服务范围2",
"pid": "0",
"isleaf": 2,
"child": [{
"name": "范围",
"pid": "999",
"id": "111111",
"isleaf": 1
}]
},
{
"id": "121324",
"name": "抚慰抚慰",
"pid": "0",
"isleaf": 1,
"child": null
},
{
"id": "1",
"name": "12345",
"pid": "0",
"isleaf": 2,
"child": [{
"name": "大道东侧1",
"pid": "1",
"id": "3",
"isleaf": 1
}]
},
{
"id": "2",
"name": "顶顶顶顶",
"pid": "0",
"isleaf": 2,
"child": [{
"name": "顶顶顶顶顶",
"pid": "2",
"id": "4",
"isleaf": 2,
"child": [{
"name": "顶顶顶顶顶顶-1",
"pid": "4",
"id": "5",
"isleaf": 1
}]
}]
}
]
最终渲染结果:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAekAAAFOCAIAAAAttXKWAAAgAElEQVR4Ae2dvWsbWdv/jx7udre8scEhCNKmSIoQGbYxGNKujAORSPGw3ZptkpDGigtHLn6Lk87plrsIUiDG2uYpAgY3C5JJ4RRpF3QHG2y23P0D9LvOmbczo9HLSBp5Rv6cwjpz3s9nlK9OrjlzrkKv11MECEAAAhDIFYH/ydVoGSwEIAABCGgCaDffAwhAAAL5I4B25++eMWIIQAACaDffAQhAAAL5I4B25++eMWIIQAACaDffAQhAAAL5I4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2CMyLANo9L9IT9GN8ChdXxq5508qPDYaCEFhAAtaShuj1EYh7raa963zf4l48uWnlB9+Zy2aZd3MG4yFnYQmw7s7o73HaPoXz3n5GbxvDgsDcCCzsr1JeJqZX0DqUm5fukM8bspJUmw3vutdzUpxN2TetfF7uI+OEwHwJ4Gt4vrxH9Ja2T+G8tz8CH9kQuDkE8DVsFr38gQAEIJArAti7c3W7GCwEIAABQwDt5osAAQhAIH8E0O783TNGDAEIQADt5jsAAQhAIH8E0O783TNGDAEIQADt5jsAAQhAIH8E0O783TNGDAEIQADt5jsAAQhAIH8E0O783TNGDAEIQADt5jsAAQhAIH8E0O783TNGDAEIQADt5jsAAQhAIH8E0O783TNGDAEIQADt5jsAAQhAIH8E0O783TNGDAEIQADt5jsAAQhAIH8E0O783TNGDAEIQADt5jsAAQhAIH8E0O783TNGDAEIQADt5jsAAQhAIH8E0O783TNGDAEIQADt5jsAAQhAIH8E0O783TNGDAEIQADt5jsAAQhAIH8E0O783TNGDAEIQADt5jsAAQhAIH8E/j/ejAIEhxwWvAAAAABJRU5ErkJgggA=" alt="" />
具体实现:
<!DOCTYPE html>
<html> <head>
<title></title>
</head> <body>
<div class="dd"></div>
</body>
<script type="text/javascript">
var arry = [{
"id": "11",
"name": "huanhuan",
"pid": "0",
"isleaf": 2,
"child": [{
"name": "豆豆",
"pid": "22",
"id": "222",
"isleaf": 1
},
{
"name": "huanhuanhuan",
"pid": "2323",
"id": "2324",
"isleaf": 1
}
]
},
{
"id": "1212",
"name": "大大",
"pid": "0",
"isleaf": 2,
"child": [{
"name": "1234",
"pid": "1313",
"id": "4444",
"isleaf": 1
}]
},
{
"id": "666",
"name": "光头强爱上熊二",
"pid": "0",
"isleaf": 1,
"child": null
},
{
"id": "888",
"name": "服务范围2",
"pid": "0",
"isleaf": 2,
"child": [{
"name": "范围",
"pid": "999",
"id": "111111",
"isleaf": 1
}]
},
{
"id": "121324",
"name": "抚慰抚慰",
"pid": "0",
"isleaf": 1,
"child": null
},
{
"id": "1",
"name": "12345",
"pid": "0",
"isleaf": 2,
"child": [{
"name": "大道东侧1",
"pid": "1",
"id": "3",
"isleaf": 1
}]
},
{
"id": "2",
"name": "顶顶顶顶",
"pid": "0",
"isleaf": 2,
"child": [{
"name": "顶顶顶顶顶",
"pid": "2",
"id": "4",
"isleaf": 2,
"child": [{
"name": "顶顶顶顶顶顶-1",
"pid": "4",
"id": "5",
"isleaf": 1
}]
}]
}
]
var html = ""; function orgTreeFun(data) {
html += "<ul>";
if (data.length > 0) {
for (var i = 0; i < data.length; i++) {
if (data[i].child) {
html += "<li id=" + data[i].id + ">" + data[i].name;
orgTreeFun(data[i].child);
html += "</li>";
} else {
html += "<li id=" + data[i].id + ">" + data[i].name + "</li>";
}
}
html += '</ul>';
}
}
orgTreeFun(arry);
document.getElementsByClassName('dd')[0].innerHTML=html;
</script>
</html>
数据格式:
var menuArry = [
{ id: 1, name: "办公管理", pid: 0 },
{ id: 2, name: "请假申请", pid: 1 },
{ id: 3, name: "出差申请", pid: 1 },
{ id: 4, name: "请假记录", pid: 2 },
{ id: 5, name: "系统设置", pid: 0 },
{ id: 6, name: "权限管理", pid: 5 },
{ id: 7, name: "用户角色", pid: 6 },
{ id: 8, name: "菜单设置", pid: 6 },
];
渲染结果:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAARsAAADpCAIAAACbYgTQAAAd40lEQVR4Ae1dv2scO7uevZz23PJgg01YSOsiLkLWcBpDIMXXxIsPZJcUIV3cOSaN7RT+UdwQ0rkMtwjeQMLuab4iYHDzgTeVU6QNbEIMNqe8/8De95VGM9KMZndmdmczs3pU2BqNpJEe6dlXP169qg2HQw8OCACBKSHwX1PKB9kAASDACIBR6AdAYJoIgFHTRBN5AQEwCn0ACEwTATBqmmgiLyAARqEPAIFpIgBGTRNN5AUEwCj0ASAwTQTAqGmiibyAABiFPgAEponAzBl11WnWjvvTrALndfO+Was1O1dmvp+PLYFmFDwBgekiMHNGZSv+TeevWu1QEpD9zfc3lgy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UAGOVWe6O2RSMARhWNMPJ3CwEwyq32Rm2LRgCMKhph5O8WAmCUW+2N2haNABhVNMLI3y0E/h9G7NMSkRZ40QAAAABJRU5ErkJgggA=" alt="" />
具体实现:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"> <head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<script src="https://cdn.bootcss.com/jquery/1.11.0/jquery.min.js"></script>
</head> <body>
<script type="text/javascript">
$(function() {
var menuArry = [
{ id: 1, name: "办公管理", pid: 0 },
{ id: 2, name: "请假申请", pid: 1 },
{ id: 3, name: "出差申请", pid: 1 },
{ id: 4, name: "请假记录", pid: 2 },
{ id: 5, name: "系统设置", pid: 0 },
{ id: 6, name: "权限管理", pid: 5 },
{ id: 7, name: "用户角色", pid: 6 },
{ id: 8, name: "菜单设置", pid: 6 },
]; GetData(0, menuArry)
$("body").append(menus);
}); //菜单列表html
var menus = ''; //根据菜单主键id生成菜单列表html
//id:菜单主键id
//arry:菜单数组信息
function GetData(id, arry) {
var childArry = GetParentArry(id, arry);
console.log(childArry);
if (childArry.length > 0) {
menus += '<ul>';
for (var i in childArry) {
menus += '<li>' + childArry[i].name;
GetData(childArry[i].id, arry);
menus += '</li>';
}
menus += '</ul>';
}
} //根据菜单主键id获取下级菜单
//id:菜单主键id
//arry:菜单数组信息
function GetParentArry(id, arry) {
var newArry = new Array();
for (var i in arry) {
if (arry[i].pid == id)
newArry.push(arry[i]);
}
return newArry;
}
</script>
</body> </html>
遍历json数组实现树的更多相关文章
-
jquery $.each遍历json数组方法
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/ ...
-
JQuery遍历json数组的3种方法
这篇文章主要介绍了JQuery遍历json数组的3种方法,本文分别给出了使用each.for遍历json的方法,其中for又分成两种形式,需要的朋友可以参考下 一.使用each遍历 $(functio ...
-
如何利用JavaScript遍历JSON数组
1.设计源码 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www. ...
-
$.each遍历json数组
1.遍历单层json数组 我们把idx和obj都打印出来看看,到底是什么东西 var json1 =[{"id":"1","tagName" ...
-
jquery $.each()遍历json数组
使用jQuery的$.each()方法来遍历一个数组对象 var json=[ {"id":"1","tagName":"appl ...
-
Json格式循环遍历,Json数组循环遍历
Json格式数据如何遍历,这里我们可以用for..in实现 例如最简单的json格式 , 'handsome' : 'yes' }; for( var key in json1 ){ console. ...
-
jquery遍历获取带checkbox表格的选中值以及遍历json数组
今天整理了一下jquery遍历的两个用法,分享给大家. 1.$().each 主要用来遍历DOM元素,获取DOM的值或样式等. 2.$.each() 主要用来遍历后台ajax返回的json数组,循环将 ...
-
【转】 jquery遍历json数组方法
$(function () { var tbody = ""; //------------遍历对象 .each的使用------------- //对象语法JSON数据格式(当服 ...
-
jQuery遍历Json数组
var jsonArray= [{ "name": "张三", "password": "123456"},{ &qu ...
随机推荐
-
xss漏洞修复,待完善
1.防止sql注入 /// <summary> /// 分析用户请求是否正常 /// </summary> /// <param name="Str" ...
-
Android 中onSaveInstanceState和onRestoreInstanceState学习
1. 基本作用: Activity的 onSaveInstanceState() 和 onRestoreInstanceState()并不是生命周期方法,它们不同于 onCreate().onPaus ...
-
MQTT V3.1--我的理解
最近因为工作需要,需要对推送消息了解,因此对MQTT进行了整理,这里更多的是对MQTT英文版的翻译和理解. MQTT(Message Queue Telemetry Transport),遥测传输协议 ...
-
JavaScript正则表达式下——相关方法
上篇博客JavaScript 正则表达式上——基本语法介绍了JavaScript正则表达式的语法,有了这些基本知识,可以看看正则表达式在JavaScript的应用了,在一切开始之前,看看RegExp实 ...
-
Macbook之用brew安装Python
1. brew install python 2.If you don't have ~/.bash_profile, add ~/.bash_profile by touch ~/.bash_pro ...
-
使用SeaJS实现模块化JavaScript开发
前言 SeaJS是一个遵循CommonJS规范的JavaScript模块加载框架,可以实现JavaScript的模块化开发及加载机制.与jQuery等JavaScript框架不同,SeaJS不会扩展封 ...
-
GDKOI 2016
GDKOI 2016 day 1 第一题 魔卡少女 题目描述:维护一个序列,能进行以下两个操作:1.将某一个位置的值改变.2.求区间的连续子串的异或值的和. solution 因为序列的数的值都小于\ ...
-
操作百度API
string json = ""; try { //虽然两者都是异步请求事件,但是WebClient是基于事件的异步,而HttpWebRequst是基于代理的异步编程 WebCli ...
-
Tree--RedBlackTree详解(2 - 3 - 4Tree)(红黑树)
#topics h2 { background: #2B6695; color: #FFFFFF; font-family: "微软雅黑", "宋体", &qu ...
-
Sublime Text 3 Build 3143 可用License
—– BEGIN LICENSE —–TwitterInc200 User LicenseEA7E-8900071D77F72E 390CDD93 4DCBA022 FAF6079061AA12C0 ...