feat: 增加将列表数据转换为树形结构的能力

This commit is contained in:
2025-11-27 12:44:06 +08:00
parent 98f7414d9e
commit b0d0a6db28
2 changed files with 79 additions and 1 deletions

34
op_array/util_test.go Normal file
View File

@ -0,0 +1,34 @@
// Package op_array ...
//
// Description : op_array ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2025-11-27 12:40
package op_array
import (
"testing"
"git.zhangdeman.cn/zhangdeman/serialize"
)
func TestTree(t *testing.T) {
type Data struct {
ID int `json:"id" dc:"数据ID"`
ParentID int `json:"parent_id" dc:"父级ID"`
Value string `json:"value" dc:"数据值"`
}
dataList := []Data{
{ID: 1, ParentID: 0, Value: "1"},
{ID: 2, ParentID: 1, Value: "2"},
{ID: 3, ParentID: 1, Value: "3"},
{ID: 4, ParentID: 2, Value: "4"},
{ID: 5, ParentID: 2, Value: "5"},
{ID: 6, ParentID: 3, Value: "6"},
{ID: 7, ParentID: 3, Value: "7"},
}
res := Tree(dataList, func(v Data) int { return v.ParentID }, func(v Data) int { return v.ID })
serialize.JSON.ConsoleOutput(res)
}