dept 结构:
[
{
"id": "2",
"name": "aaa",
"children": [
{
"id": "3",
"name": "bbb",
"children": [
{
"id": "4",
"name": "ccc",
"children": [
]
},
{
"id": "5",
"name": "ddd",
"children": [
]
}
]
},
{
"id": "6",
"name": "eee",
"children": [
]
}
]
},
{
"id": "7",
"name": "fff",
"children": [
]
}
]
user 数据结构:
[
{
"id": 2,
"name": "aaa",
"department": 6
},
{
"id": 1,
"name": "admin",
"department": 7
}
]
合并后的结构:
[
{
"id": "2",
"name": "aaa",
"children": [
{
"id": "3",
"name": "bbb",
"children": [
{
"id": "4",
"name": "ccc",
"children": [
]
},
{
"id": "5",
"name": "ddd",
"children": [
]
}
]
},
{
"id": "6",
"name": "eee",
"children": [
],
"users": [
{
"id": 2,
"name": "aaa"
},
]
}
]
},
{
"id": "7",
"name": "fff",
"children": [
],
"users": [
{
"id": 1,
"name": "admin"
},
]
}
]
我目前的做法是遍历一遍 dept 列表,找到了一个 dept 就遍历以便 user 列表,如果用户多的话,这样就很慢了…… 有什么更好的办法吗?
1
dayeye2006199 2021-06-28 15:39:13 +08:00 1
弄个 dept -> user 的 map,是不是就快一些了
|
2
tomkliyes 2021-06-28 15:53:14 +08:00
先遍历一遍 user 得到{dept: users},再遍历 dept
|
3
lanshee 2021-06-29 10:56:55 +08:00
时间空间二选一,想要时间快就 map,空间小就 list.
|