当前位置:网站首页>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": ""
}
]
},
边栏推荐
- SQL函数 $TRANSLATE
- 力扣209-长度最小的字符串——滑动窗口法
- Likou 35 - search for insertion position - binary search
- Running yum reports Error: Cannot retrieve metalink for reposit
- 基于threejs的商品VR展示平台的设计与实现思路
- 您应该知道的 Google Sheets 使用技巧
- 力扣977-有序数组的平方——暴力法&双指针法
- WPF 实现窗体抖动效果
- openresty 性能优化
- Problem solving in the process of using mosquitto
猜你喜欢
随机推荐
ssm web page access database data error
npm run dev 和 npm run serve区别
5G网络切片技术
SQL function TRIM
元宇宙“吹鼓手”Unity:疯狂扩局,悬念犹存
半夜赶工制作简报的我好想说 : 确定了,最终稿就是这样
华为eNSP(基础实验通信)
当POC遇见RPA:RPA项目顺利实施的关键
流动性质押挖矿系统开发如何制作?单双币系统开发成熟技术
ansible module --yum module
jvmxmx和xms参数分析(设定优化校准)
Learning Experience Sharing Seven: YOLOv5 Code Chinese Comments
go语言的接口
运行yum报错Error: Cannot retrieve metalink for reposit
图形处理单元(GPU)的演进
今日睡眠质量记录85分
ABAP-OOAVL模板程序
云原生(三十) | Kubernetes篇之应用商店-Helm介绍
CCF论文会议 IEEE 如何查询某个会议期刊的所有文章
openresty 性能优化