<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<h1>我是建爱你关系人</h1>
<script>
let arr1 = [
{
icon: "home",
id: 100,
pid: 10,
resourceName: "首页"
},
{
icon: "home",
id: 101,
pid: 10,
resourceName: "新闻"
},
{
icon: "home",
id: 102,
pid: 10,
resourceName: "导航"
},
{
icon: "home",
id: 103,
pid: 10,
resourceName: "我的"
}
]
let arr2 = [
{
icon: "home",
id: 1001,
pid: 100,
resourceName: "首页1"
},
{
icon: "home",
id: 1002,
pid: 100,
resourceName: "首页2"
},
{
icon: "home",
id: 1003,
pid: 100,
resourceName: "首页3"
},
{
icon: "home",
id: 1011,
pid: 101,
resourceName: "新闻1"
},
{
icon: "home",
id: 1012,
pid: 101,
resourceName: "新闻2"
},
{
icon: "home",
id: 1013,
pid: 101,
resourceName: "新闻3"
},
{
icon: "home",
id: 1021,
pid: 102,
resourceName: "导航1"
},
{
icon: "home",
id: 1022,
pid: 102,
resourceName: "导航2"
},
{
icon: "home",
id: 1023,
pid: 102,
resourceName: "导航3"
},
{
icon: "home",
id: 1031,
pid: 103,
resourceName: "我的1"
},
{
icon: "home",
id: 1032,
pid: 103,
resourceName: "我的2"
},
{
icon: "home",
id: 1033,
pid: 103,
resourceName: "我的3"
}
]
let arr3 = [
{
icon: "home",
id: 10011,
pid: 1001,
resourceName: "首页"
},
{
icon: "home",
id: 10111,
pid: 1011,
resourceName: "新闻"
},
{
icon: "home",
id: 10211,
pid: 1021,
resourceName: "导航"
},
{
icon: "home",
id: 10311,
pid: 1031,
resourceName: "我的"
}
]
function sum(first, second) {
first.map(item => {
item.children=[]
second.map(jtem => {
if (item.id === jtem.pid) {
item.children.push(jtem)
}
})
})
return first
}
let y = sum(arr1, arr2)
let s = sum(arr2, arr3)
console.log(y)
</script>
</body>
</html>
递归 数据重组
于 2021-11-19 17:57:09 首次发布