简单 递归取到数组子数组的值

<!DOCTYPE html>
<html>

<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible"
content="IE=edge">
<title>Page Title</title>
<meta name="viewport"
content="wIdth=device-wIdth, initial-scale=1">
<link rel="stylesheet"
type="text/css"
media="screen" />
<script>
var items = [
{
Id: 1,
name: '产品部',
Children: []
},
{
Id: 2,
name: '项目部',
Children: [
{
Id: 4,
name: '项目部1',
Children: [
{
Id: 6,
name: '项目部111',
Children: []
},
]
},
{
Id: 5,
name: '项目部2',
Children: []
},
]
},
{
Id: 3,
name: '测试部',
Children: []
},
]
var a = [2,5]

function factorial(departmentList, departmentIds) {
for (let j = 0; j < departmentIds.length; j++) {
for (let i = 0; i < departmentList.length; i++) {
if (departmentIds[j] == departmentList[i].Id) {
if (j == departmentIds.length - 1) {
return JSON.stringify(departmentList[i]);
}
return this.factorial(departmentList[i].Children, departmentIds);
}
}
}
}
console.log(this.factorial(items, a));



</script>
</head>

<body>

</body>

</html>
原文地址:https://www.cnblogs.com/xinhang/p/8082201.html