我有一个名称的数据线性,例如:
名称:一个,ID:1,ParentId:0
名称:一对一,ID:2,ParentId:1
名称:一对一,ID:3,ParentId:2
名称:一一二,ID:4,ParentId:2
例如这个数据,我从数据库中获取,但我想测试我制作虚拟数据来构造的逻辑。我想我为数据递归地做了一个临时索引。如果地图中不存在数据,我会设置,如果数据必须附加到切片之前,我会得到索引。但是,我认为在函数递归中(我在下面显示),它不起作用(数据不附加)。为什么?有没有错误的算法逻辑?
我的结果的正确解决方案是什么
[
{
"id": 1,
"name": "One",
"children": [
{
"id": 2,
"name": "One-One",
"children": [
{
"id": 3,
"name": "One-One-One",
"children": null
},
{
"id": 4,
"name": "One-One-Two",
"children": null
}
]
}
]
}
]
golang 中的完整代码:
package main
import (
"encoding/json"
"fmt"
)
type Data struct {
Id int `json:"id"`
ParentId int `json:"parent_id"`
Name string `json:"name"`
}
type Datas []Data
type Response struct {
Id int `json:"id"`
Name string `json:"name"`
Children Responses `json:"children"`
}
type Responses []*Response
func main() {
datas := Datas{
{
Name: "One",
Id: 1,
},
{
Name: "One-One",
Id: 2,
ParentId: 1,
},
{
Name: "One-One-One",
Id: 3,
ParentId: 2,
},
{
Name: "One-One-Two",
Id: 4,
ParentId: 2,
},
}
var result Responses
tempIdx := make(map[int]int)
for _, val := range datas {
res := Response{
Id: val.Id,
Name: val.Name,
}
if val.ParentId == 0 {
result = append(result, &res)
tempIdx[val.Id] = len(result) - 1
continue
} else {
recursive(val.ParentId, result, res, tempIdx)
}
}
json, err := json.Marshal(result)
if err != nil {
panic(err)
}
fmt.Println(string(json))
}
阿晨1998