<!DOCTYPE html>
<head>
<meta charset="utf-8">
<title>ua</title>
</head>
<body>
</div>
<script type="text/javascript">
//递归时 href,层级 结果
var data = [
{
"resName": "ETC管理",
"powerCode": 2060001,
"href": "1"
},
{
"resName": "保险",
"child": [
{
"resName": "放空险管理",
"powerCode": 2060002,
"href": "2"
},
{
"resName": "放空险管理",
"powerCode": 2060002,
"child": [
{
"resName": "放空险管理",
"powerCode": 206000,
"href": "3"
},
{
"resName": "放空险管理",
"powerCode": 2060002,
"href": "4"
},
{
"resName": "放空险管理",
"powerCode": 2060002,
"href":"5"
}
]
},
{
"resName": "放空险管理",
"powerCode": 2060002,
"href": "7"
}
]
},
{
"powerCode": 2060003,
"resName": "广告管理",
"href": "8"
}
];
//拿到数组值
function addExpand(arr, find){
var crr = {};
var i=0;
for(var j in arr){
var item = arr[j];
if (j == find) {
crr[j] = arr[j];
}
if(typeof item === 'object'){
crr[j] = addExpand(item, find);
}
}
return crr;
}
var b = addExpand(data, 'href');
console.log(b)
console.log(JSON.stringify(b));
//拿出对应的层级数
var d = {};
function deep(arr,n=1){
var c =n;
for(var j in arr){
var n=0;
if(Object.keys(arr[j]) !="href"){
n++;
deep(arr[j].child,n+c);
}else{
d[arr[j].href] = c+n;
n=0;
}
}
return d;
}
console.log(deep(b))
</script>
</body>
</html>
找出json数据里面的href值并且算出它的层级
最新推荐文章于 2023-01-02 21:27:42 发布