mirror of
https://gitee.com/xiangheng/x_admin.git
synced 2025-10-05 08:07:06 +08:00
init
This commit is contained in:
38
server/util/array.go
Normal file
38
server/util/array.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package util
|
||||
|
||||
var ArrayUtil = arrayUtil{}
|
||||
|
||||
//arrayUtil 数组工具类
|
||||
type arrayUtil struct{}
|
||||
|
||||
//ListToTree 字典列表转树形结构
|
||||
func (au arrayUtil) ListToTree(arr []map[string]interface{}, id string, pid string, child string) (mapList []interface{}) {
|
||||
mapList = []interface{}{}
|
||||
// 遍历以id_为key生成map
|
||||
idValMap := make(map[uint]interface{})
|
||||
for _, m := range arr {
|
||||
if idVal, ok := m[id]; ok {
|
||||
idValMap[idVal.(uint)] = m
|
||||
}
|
||||
}
|
||||
// 遍历
|
||||
for _, m := range arr {
|
||||
// 获取父节点
|
||||
if pidVal, ok := m[pid]; ok {
|
||||
if pNode, pok := idValMap[pidVal.(uint)]; pok {
|
||||
// 有父节点则添加到父节点子集
|
||||
if cVal, cok := pNode.(map[string]interface{})[child]; cok {
|
||||
if cVal == nil {
|
||||
cVal = []interface{}{m}
|
||||
} else {
|
||||
cVal = append(cVal.([]interface{}), m)
|
||||
}
|
||||
pNode.(map[string]interface{})[child] = cVal
|
||||
continue
|
||||
}
|
||||
}
|
||||
}
|
||||
mapList = append(mapList, m)
|
||||
}
|
||||
return
|
||||
}
|
Reference in New Issue
Block a user