当前位置:网站首页>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": ""
}
]
},
边栏推荐
- Create an application operation process using the kubesphere GUI
- 【2022 小目标检测综述】Towards Large-Scale Small Object Detection: Survey and Benchmarks
- yolo格式(txt)数据集转VOC(xml)
- 智能手表前景如何?
- Pytorch 占用cpu资源过多
- 华为eNSP(基础实验通信)
- AQS-AbstractQueuedSynchronizer
- 基于深度学习的裂缝检测技术
- ssm网页访问数据库数据报错
- Likou 977-Squaring of ordered arrays - brute force method & double pointer method
猜你喜欢
Likou 58 - Left Rotation String
Mysql transaction isolation level and MVCC (multi-version concurrency control)
Thymeleaf
Likou 977-Squaring of ordered arrays - brute force method & double pointer method
Create a devops CI/CD process using the kubesphere GUI
Problem solving in the process of using mosquitto
npm run dev 和 npm run serve区别
Crack detection technology based on deep learning
ssm网页访问数据库数据报错
使用mosquitto过程中的问题解决
随机推荐
翻译英语的软件-免费翻译软件-各种语言互相翻译
Create an application operation process using the kubesphere GUI
故障分析 | 一条 SELECT 语句跑崩了 MySQL ,怎么回事?
“纯C”实现——三子棋小游戏
【kali-信息收集】(1.9)Metasploit+搜索引擎工具Shodan
【MySQL】多表联合查询、连接查询、子查询「建议收藏」
看我如何用多线程,帮助运营小姐姐解决数据校对系统变慢!
sqli-labs(less-11)
阿苹的思考
CCF论文会议 IEEE 如何查询某个会议期刊的所有文章
面积曲线AUC(area under curve)
go源码之sync.Waitgroup
SQL 数据更新
使用mosquitto过程中的问题解决
ASP.NET Core 6框架揭秘实例演示[31]:路由“高阶”用法
Likou 977-Squaring of ordered arrays - brute force method & double pointer method
Crack detection technology based on deep learning
ansible模块--copy模块
Breaking the Boundary, Huawei's Storage Journey
npm install报错npm ERR Could not resolve dependency npm ERR peer