当前位置:网站首页>[go] go implements row column conversion of sets
[go] go implements row column conversion of sets
2022-07-01 01:13:00 【CTRA Wang dada】
One 、 Scene one
1、 Fixed column header row to column
Fixed column header row to column : Explain it. , Converted column headers ( namely : Yesterday's date 、 Today's date ) For fixed , We don't have to deal with it
demand : user A There are 3 Accounts (account) Then each account generates a certain fee every day , Then we extract data and need to show Here's the picture :
Here we will A Different users account A comparison is made between today's and yesterday's expenses In this scenario , We need to perform an operation of transferring data from row to column
2、 Row to column method (map Array )
If the data type we output is :res := []map[string]interface{}{} namely :map Array
Row to column conversion requires a unique reference column , As shown in the figure above account For reference columns
The input parameters are : Output array 、map Of key
The return results are :key Whether there is 、 There is map、 There is map The index of
// MapKeyExist Based on the incoming map In the list , Determine whether there is a certain map Contains the key
func MapKeyExist(list []map[string]interface{
}, key string) (bool, map[string]interface{
}, int) {
//fmt.Println(len(list))
if len(list) > 0 {
for i, m := range list {
//fmt.Println(m)
if _, ok := m[key]; ok {
return true, m, i
}
}
}
return false, nil, 0
}
3、 How to use
Here is a pseudocode , Briefly introduce the idea of row column conversion Please read each line carefully , If there are questions , Leave a comment in the comments section
res := []map[string]interface{
}{
}
if len( Fetched from database list) > 0 {
for _, item := range Fetched from database list {
// Determine whether the unique value exists in the array ( Use here account)
flag, data, index := maputil.MapKeyExist(res, item.Account)
// If there is
if flag == true {
// Get the currently existing object from the array
resTemp := res[index]
// Add the required fields below
....
} else {
// Initialize a map
temp := make(map[string]interface{
})
// Set a unique column here , As a reference for later consolidation
temp[item.Account] = item.Account
// The required columns can be completed during insertion
temp["xxx"] = 0 // int Give default
temp["kkk"] = ""// string Give default
// Add to map Array
res = append(res, temp)
}
}
}
Two 、 Scene two
1、 Custom column header row to column
Custom column header row to column : Explain it. , That is, we will see the left side of the figure below account number The row is converted to the column header on the right of the following figure
demand : user 103 There are 2 Accounts , Then each account will incur a certain fee every day , Then we extract data to show the specific cost of each account according to the service Here's the picture :
2、 Row to column method
The way of thinking is different from that of scenario 1 above
If we want to show the following data structure ( namely : Do not show the user column above )
3、 How to use
Analysis of the table
We need to put the header Th The above needs to dynamically obtain data
then table Of Tbody in
3.1 assemble th Ideas
First of all, we should make clear what to render th It's an array , Here we use the data structure of the array Definition th := []string{}
A piece of pseudo code is as follows :
th := []string{
}
if len(res) > 0 {
for _, temp1 := range res {
index := arrayUtil.In(temp1.Account, th)
if index == false {
th = append(th, temp1.Account)
}
}
}
}
3.2 obtain Tbody Ideas
Clear the header , We will face that every line is a business name ( Name :service Corresponding data ), And the data of each business should correspond to account( dynamic th Header ), Finally, we choose map in string- Array data structure
Such as :
{
“service1”:[1,2,3,4,5]
} Definition tbody := map[string][]interface{}{}
if len(res) > 0 {
for _, temp := range res {
tbody[temp.Service] = append(tbody[temp.Service], totalNum)
}
}
Best practices :
- above append I have tested , If tbody This map There is no such string Of key Will be automatically created and assigned
- If tbody This map There is this in key, I'll tell you again key Add elements to the corresponding array
- So you can use it safely
3.3 summary
above 2 There is a res Set
Why don't we consider th and tbody The order of , Because the same res, We don't have to think about
If there is no need for other special processing data , We can 3.1 and 3.2 Put them together
边栏推荐
- NE555波形发生器手把手教程之NE555内部结构(一)
- HDU 2488 A Knight's Journey(DFS)
- ORB-SLAM2源码学习(二)地图初始化
- 获取屏幕高度
- 运动与健康
- Unhandled Exception: MissingPluginException(No implementation found for method launch on channel)
- CSDN common complex formula template record
- Join table query select generation
- Error msb8031: building an MFC project for a non Unicode character set is deprecated
- 软硬件基础知识学习--小日记(1)
猜你喜欢
随机推荐
Get to know the drawing component of flutter - custompaint
Problem solving: how to manage thread_local pointer variables
The communication mechanism and extension of Supervisor
ArrayList analysis 1-cycle, capacity expansion, version
Mindjet mindmanager2022 mind map decompression installer tutorial
Tcp/ip protocol stack, about TCP_ RST | TCP_ ACK correct attitude
mustache语法
Join table query select generation
CMU15445 (Fall 2019) 之 Project#1 - Buffer Pool 详解
PHP online confusion encryption tutorial sharing + basically no solution
HDU 2488 A Knight's Journey(DFS)
5. TPM module initialization
Listview in flutter application development
Double linked list: initialize insert delete traversal
None of the following candidates is applicable because of a receiver type mismatch
[original] PLSQL index sorting optimization
[LeetCode] 两数之和【1】
2022就要过去一半了,挣钱好难
分割链表[先取next再斩断链表防止断链]
探索互联网时代STEAM教育创新之路




![奇偶链表[链表操作的两种大方向]](/img/4e/ce860bc172bb75f456427ba26a7842.png)




