当前位置:网站首页>According to the field classification Golang map array
According to the field classification Golang map array
2022-08-02 11:46:00 【great wash】
Go Class 2D array sorted by field/mapArrays are sorted by fields
原始数据
Data corresponding structure
type IgmsMenu struct {
ID uint `gorm:"column:id;" json:"id"`
CategoryId int64 `gorm:"column:category_id;" json:"category_id"`
Name string `gorm:"column:name;" json:"name"`
Price *decimal.Decimal `gorm:"column:price;type:decimal" json:"price"`
Remark string `gorm:"column:remark;" json:"remark"`
Status int8 `gorm:"column:status;default:0;" json:"status"`
}raw data is returnedjson数据如下:
"data": [
{
"id": 1,
"category_id": 6,
"name": "凉拌牛肉",
"price": "45",
"remark": "",
"status": 0
},
{
"id": 2,
"category_id": 7,
"name": "Beef Dumplings",
"price": "17",
"remark": "",
"status": 0
},
{
"id": 3,
"category_id": 7,
"name": "Beef soup dumplings",
"price": "17",
"remark": "",
"status": 0
},
{
"id": 4,
"category_id": 7,
"name": "酸汤饺子",
"price": "18",
"remark": "",
"status": 0
},
{
"id": 5,
"category_id": 7,
"name": "braised dumplings",
"price": "19",
"remark": "",
"status": 0
}
],数据处理
需求
need according to the datacategory_idto do array classification.
原理
因category_id的数据类型为int64,So you need to define a type as map[int64][]map[string]interface{}to accept the processed data.
- map[int64]:This layer is used to undertake various types of array sets after classification
- []map[string]interface{}:An array of data for a single class
代码
func LauwenDeal(infos []model.IgmsMenu) map[int64][]map[string]interface{} {
res := make(map[int64][]map[string]interface{})
for _, item := range infos {
temp := map[string]interface{}{
"id": item.ID,
"name": item.Name,
"price": item.Price,
"remark": item.Remark,
}
res[0] = append(res[0], temp)
res[item.CategoryId] = append(res[item.CategoryId], temp)
}
return res
}处理结果
处理后返回的json数据
"data": {
"6": [
{
"id": 1,
"name": "凉拌牛肉",
"price": "45",
"remark": ""
}
],
"7": [
{
"id": 2,
"name": "Beef Dumplings",
"price": "17",
"remark": ""
},
{
"id": 3,
"name": "Beef soup dumplings",
"price": "17",
"remark": ""
},
{
"id": 4,
"name": "酸汤饺子",
"price": "18",
"remark": ""
},
{
"id": 5,
"name": "braised dumplings",
"price": "19",
"remark": ""
}
]
},边栏推荐
猜你喜欢
随机推荐
WPF 实现窗体抖动效果
力扣27-移除元素——简单题
故障分析 | 一条 SELECT 语句跑崩了 MySQL ,怎么回事?
5G网络切片技术
npm run dev 和 npm run serve区别
网站自动翻译-网站批量自动翻译-网站免费翻译导出
记录代码
观察者(observer)模式(二) —— 实现线程安全的监听器
Coroutines and Lifecycle in Kotlin
当POC遇见RPA:RPA项目顺利实施的关键
SQL function TRIM
翁恺C语言程序设计网课笔记合集
【kali-信息收集】(1.8)ARP侦查工具_Netdiscover
Golang map数组按字段分类
Learning Experience Sharing Seven: YOLOv5 Code Chinese Comments
QT笔记——在一个窗口上显示另外一个透明窗口
Idea 全局搜索(idea如何全局搜索关键字)
注意力机制
SQL函数 $TRANSLATE
雷克萨斯,锁死的安全,挡不住的心寒









