当前位置:网站首页>Golang map数组按字段分类
Golang map数组按字段分类
2022-08-02 11:40:00 【棒洗啦】
Go 类二维数组按字段分类/map数组按字段分类
原始数据
数据相应结构体
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"`
}原始数据返回的json数据如下:
"data": [
{
"id": 1,
"category_id": 6,
"name": "凉拌牛肉",
"price": "45",
"remark": "",
"status": 0
},
{
"id": 2,
"category_id": 7,
"name": "牛肉干饺",
"price": "17",
"remark": "",
"status": 0
},
{
"id": 3,
"category_id": 7,
"name": "牛肉汤饺",
"price": "17",
"remark": "",
"status": 0
},
{
"id": 4,
"category_id": 7,
"name": "酸汤饺子",
"price": "18",
"remark": "",
"status": 0
},
{
"id": 5,
"category_id": 7,
"name": "烩饺",
"price": "19",
"remark": "",
"status": 0
}
],数据处理
需求
需要根据数据中的category_id来做数组分类。
原理
因category_id的数据类型为int64,所以需要定义一个类型为map[int64][]map[string]interface{}的来承接处理后的数据。
- map[int64]:这层用来承接分类后的各类数组集
- []map[string]interface{}:单个类的数据数组
代码
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": "牛肉干饺",
"price": "17",
"remark": ""
},
{
"id": 3,
"name": "牛肉汤饺",
"price": "17",
"remark": ""
},
{
"id": 4,
"name": "酸汤饺子",
"price": "18",
"remark": ""
},
{
"id": 5,
"name": "烩饺",
"price": "19",
"remark": ""
}
]
},边栏推荐
- ansible模块--copy模块
- Getting Started with Three.JS Programmatic Modeling
- 智能手表前景如何?
- SQLAlchemy使用教程
- npm WARN deprecated [email protected] This version of tar is no longer supported, and will not receive
- Outsourced Student Management System Architecture Documentation
- 免费的中英文翻译软件-自动批量中英文翻译软件推荐大全
- “纯C”实现——三子棋小游戏
- 细学常用类,集合类,IO流
- 记录代码
猜你喜欢

ssm web page access database data error

Mysql事务隔离级别与MVCC(多版本并发控制)

The exchange - string dp

LeetCode每日一练 —— 20. 有效的括号

面积曲线AUC(area under curve)

Breaking the Boundary, Huawei's Storage Journey

大疆P4M云遮挡矫正

企业级数据治理工作怎么开展?Datahub这样做

免费文档翻译-免费批量文档翻译软件推荐

Outsourced Student Management System Architecture Documentation
随机推荐
ansible module --yum module
openresty 性能优化
ansible模块--copy模块
Running yum reports Error: Cannot retrieve metalink for reposit
The sitcom "Re-Walking the Long March" was staged
Failed to configure mysql, what's going on?
List排序 ,取最大值最小值
ssm网页访问数据库数据报错
AdguardHome如何配置设置?我的AdguardHome配置内容过滤器拦截列表
5G网络切片技术
腾讯云云函数SCF—入门须知
MP的几种查询方式
基于深度学习的裂缝检测技术
10份重磅报告 — 展望中国数字经济未来
当POC遇见RPA:RPA项目顺利实施的关键
中原银行实时风控体系建设实践
图形处理单元(GPU)的演进
sva assertion data
喜迎八一 《社会企业开展应聘文职人员培训规范》团体标准出版发行会暨橄榄枝大课堂上线发布会在北京举行
FinClip | 来了, 2022 年 7 月更新大盘点