<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>递归函数</title>
</head>
<body>
</body>
</html>
<script>
let data = [
{
"id": 30,
"roleName": "主管",
"roleDesc": "技术负责人",
"children": [
{
"id": 101,
"authName": "商品管理",
"path": null,
"children": [
{
"id": 104,
"authName": "商品列表",
"path": null,
"children": [
{
"id": 105,
"authName": "添加商品",
"path": null
}
]
}
]
}
]
},
{
"id": 301,
"roleName": "主管1",
"roleDesc": "技术负责人1",
"children": [
{
"id": 1011,
"authName": "商品管理1",
"path": null,
"children": [
{
"id": 1041,
"authName": "商品列表1",
"path": null,
"children": [
{
"id": 1051,
"authName": "添加商品1",
"path": null
}
]
}
]
}
]
},
{
"id": 302,
"roleName": "主管1",
"roleDesc": "技术负责人2",
"children": [
{
"id": 1012,
"authName": "商品管理2",
"path": null,
"children": [
{
"id": 1042,
"authName": "商品列表2",
"path": null,
"children": [
{
"id": 1052,
"authName": "添加商品2",
"path": null
}
]
}
]
}
]
},
];
let arr = [];
function getAuthName(node){
console.log(node);
node.forEach(item => {
if(!item.children){
return arr.push(item.authName);
}
getAuthName(item.children)
});
}
getAuthName(data)
console.log(arr);