当前位置:网站首页>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": ""
}
]
},边栏推荐
猜你喜欢
随机推荐
[kali-information collection] (1.9) Metasploit + search engine tool Shodan
find查找多类型结尾文件
OSI 七层模型和TCP/IP模型及对应协议(详解)
学习经验分享之七:YOLOv5代码中文注释
微信小程序---组件开发与使用
Excel动态图制作
使用kubesphere图形界面创建一个应用操作流程
如何在 UE4 中制作一扇自动开启的大门
打破千篇一律,DIY属于自己独一无二的商城
故障分析 | 一条 SELECT 语句跑崩了 MySQL ,怎么回事?
excel 批量翻译-excel 批量函数公司翻译大全免费
JSP中include指令的功能简介说明
Several reasons why applet plugins benefit developers
When not to use () instead of Void in Swift
Problem solving in the process of using mosquitto
8大软件供应链攻击事件概述
Coroutines and Lifecycle in Kotlin
go语言的接口
npm run dev 和 npm run serve区别
Thymeleaf









