当前位置:网站首页>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": ""
}
]
},边栏推荐
猜你喜欢

CCF paper conference IEEE how to query all articles of a conference journal

npm run serve启动报错npm ERR Missing script “serve“

【Acunetix-忘记密码】

How to connect TDengine through DBeaver?

看我如何用多线程,帮助运营小姐姐解决数据校对系统变慢!

sva 断言资料

Likou 58 - Left Rotation String

CCF论文会议 IEEE 如何查询某个会议期刊的所有文章

当POC遇见RPA:RPA项目顺利实施的关键

基于深度学习的裂缝检测技术
随机推荐
Crack detection technology based on deep learning
【Acunetix-忘记密码】
Mysql transaction isolation level and MVCC (multi-version concurrency control)
使用kubesphere图形界面创建一个devops的CI/CD流程
Metaverse "Drummer" Unity: Crazy expansion, suspense still exists
AQS-AbstractQueuedSynchronizer
记录代码
ssm网页访问数据库数据报错
QListView的使用
面积曲线AUC(area under curve)
Excel dynamic chart production
yolo格式(txt)数据集转VOC(xml)
Likou 977-Squaring of ordered arrays - brute force method & double pointer method
CAN总线的AUTOSAR网络管理
匹配滤波(四种滤波器的幅频特性)
解决导出excel文件名中文乱码的问题
X86函数调用模型分析
excel 批量翻译-excel 批量函数公司翻译大全免费
借小程序容器打造自有App小程序生态
雷克萨斯,锁死的安全,挡不住的心寒