如何根据相同的值对数组进行分组

Posted

技术标签:

【中文标题】如何根据相同的值对数组进行分组【英文标题】:How to group array based on same value 【发布时间】:2021-11-28 09:43:30 【问题描述】:

我正在使用 golang,这是使用 struct 创建的 json 数据。但是我们需要使用 rooms->code 基础值对数据进行分组。

在下面的 json 数据中,需要使用 Room Code 明智地对数组进行分组。不要创建重复的 json 节点。 实际 Json 数据


    "responseStatus": "SUCCESS",
    "version": "v1.9",
    "checkIn": "2021-10-12",
    "checkOut": "2021-10-16",
    "currency": "AED",
    "hotels": [
        
            "code": "OT000000001",
            "name": "TAJ TEST HOTEL",
            "rooms": [
                
                    "code": "9011",
                    "name": "Beach Villa With Jacuzzi",
                    "rates": [
                        
                            "subSupplierId": "DC",
                            "boardCode": "FB",
                            "ratePlanCode": "9011_0_11_136_136",
                            "channel": 23,
                            "allotment": 100,
                            "price": 1469.04,
                            "cancellationPolicy": 
                                "policies": null
                            
                        
                    ]
                ,
                
                    "code": "8525",
                    "name": "Doubello",
                    "rates": [
                        
                            "subSupplierId": "DC",
                            "boardCode": "FB",
                            "ratePlanCode": "8525_0_11_136_136",
                            "channel": 23,
                            "allotment": 100,
                            "price": 4407.08,
                            "cancellationPolicy": 
                                "policies": null
                            
                        
                    ]
                ,
                
                    "code": "8525",
                    "name": "Doubello",
                    "rates": [
                        
                            "subSupplierId": "DC",
                            "boardCode": "",
                            "ratePlanCode": "8525_0_22_136_136",
                            "channel": 23,
                            "allotment": 100,
                            "price": 7345.12,
                            "cancellationPolicy": 
                                "policies": null
                            
                        
                    ]
                
            ]
        
    ],
    "remark": ""

需要转换我的实际输出


    "responseStatus": "SUCCESS",
    "version": "v1.9",
    "checkIn": "2021-10-12",
    "checkOut": "2021-10-16",
    "currency": "AED",
    "hotels": [
        
            "code": "OT000000001",
            "name": "TAJ TEST HOTEL",
            "rooms": [
                
                    "code": "9011",
                    "name": "Beach Villa With Jacuzzi",
                    "rates": [
                        
                            "subSupplierId": "DC",
                            "boardCode": "FB",
                            "ratePlanCode": "9011_0_11_136_136",
                            "channel": 23,
                            "allotment": 100,
                            "price": 1469.04,
                            "cancellationPolicy": 
                                "policies": null
                            
                        
                    ]
                ,
                
                    "code": "8525",
                    "name": "Doubello",
                    "rates": [
                        
                            "subSupplierId": "DC",
                            "boardCode": "FB",
                            "ratePlanCode": "8525_0_11_136_136",
                            "channel": 23,
                            "allotment": 100,
                            "price": 4407.08,
                            "cancellationPolicy": 
                                "policies": null
                            
                        , 
                            "subSupplierId": "DC",
                            "boardCode": "",
                            "ratePlanCode": "8525_0_22_136_136",
                            "channel": 23,
                            "allotment": 100,
                            "price": 7345.12,
                            "cancellationPolicy": 
                                "policies": null
                            
                        
                    ]
                
            ]
        
    ],
    "remark": ""

需要对"code": "8525"

上的数据进行分组

【问题讨论】:

这完全是太多的代码,请提供一个最小的,可重现的例子 根据您的请求删除了代码。只添加了实际需求数据。 不,您应该提供一个 minimal reproducible example 来证明问题所在,即介于整个程序和什么都没有之间的某个位置。 【参考方案1】:

您可以遍历rooms 并将每个唯一的room.code 存储在map 中。当您遇到以前见过的code 时,只需将其添加到rates 的数组中,这将是您在map 中的该键的值。然后按照自己的方式将新数字插入到现有数据结构中。

package main

import (
    "encoding/json"
    "fmt"
    "io/ioutil"
    "os"
)

type Response struct 
    Status   string  `json:"responseStatus"`
    Version  string  `json:"version"`
    CheckIn  string  `json:"checkIn"`
    CheckOut string  `json:"checkOut"`
    Currency string  `json:"currency"`
    Hotels   []Hotel `json:"hotels"`
    Remark   string  `json:"remark"`


type Hotel struct 
    Code  string `json:"code"`
    Name  string `json:"name"`
    Rooms []Room `json:"rooms"`


type Room struct 
    Code  string `json:"code"`
    Name  string `json:"name"`
    Rates []Rate `json:"rates"`


type Rate struct 
    SupplierID   string  `json:"subSupplierId"`
    BoardCode    string  `json:"boardCode"`
    RateCode     string  `json:"ratePlanCode"`
    Channel      int     `json:"channel"`
    Allotment    int     `json:"allotment"`
    Price        float64 `json:"price"`
    CancelPolicy Policy  `json:"cancellationPolicy"`


type Policy struct 
    Policies string `json:"policies"`


func main() 
    byt, err := ioutil.ReadFile("foo.json")
    if err != nil 
        os.Exit(1)
    

    resp := Response
    json.Unmarshal(byt, &resp)
    hotels := resp.Hotels
    newHotels := make([]Hotel, 0)
    for _, hotel := range hotels 
        newRooms := make(map[string]Room)
        for _, room := range hotel.Rooms 
            if val, ok := newRooms[room.Code]; ok 
                newRates := append(val.Rates, room.Rates...)
                val.Rates = newRates
                newRooms[room.Code] = val
             else 
                newRooms[room.Code] = room
            
        
        finalRooms := make([]Room, 0)
        for _, v := range newRooms 
            finalRooms = append(finalRooms, v)
        
        hotel.Rooms = finalRooms
        newHotels = append(newHotels, hotel)
    

    resp.Hotels = newHotels
    respJSON, err := json.MarshalIndent(resp, "", "  ")
    if err != nil 
       os.Exit(1)
    
    fmt.Printf(string(respJSON))



  "responseStatus": "SUCCESS",
  "version": "v1.9",
  "checkIn": "2021-10-12",
  "checkOut": "2021-10-16",
  "currency": "AED",
  "hotels": [
    
      "code": "OT000000001",
      "name": "TAJ TEST HOTEL",
      "rooms": [
        
          "code": "9011",
          "name": "Beach Villa With Jacuzzi",
          "rates": [
            
              "subSupplierId": "DC",
              "boardCode": "FB",
              "ratePlanCode": "9011_0_11_136_136",
              "channel": 23,
              "allotment": 100,
              "price": 1469.04,
              "cancellationPolicy": 
                "policies": ""
              
            
          ]
        ,
        
          "code": "8525",
          "name": "Doubello",
          "rates": [
            
              "subSupplierId": "DC",
              "boardCode": "FB",
              "ratePlanCode": "8525_0_11_136_136",
              "channel": 23,
              "allotment": 100,
              "price": 4407.08,
              "cancellationPolicy": 
                "policies": ""
              
            ,
            
              "subSupplierId": "DC",
              "boardCode": "",
              "ratePlanCode": "8525_0_22_136_136",
              "channel": 23,
              "allotment": 100,
              "price": 7345.12,
              "cancellationPolicy": 
                "policies": ""
              
            
          ]
        
      ]
    
  ],
  "remark": ""
            

【讨论】:

以上是关于如何根据相同的值对数组进行分组的主要内容,如果未能解决你的问题,请参考以下文章

ReactJS:如何根据道具的值对对象数组进行排序?

如何根据特定行中的值对 numpy 数组进行排序?

如何根据它们指向的值对双指针数组进行排序?

如何在 Freemarker 中按特定值对列表进行分组?

2维数组根据里面某个相同的键值对分组

根据每个值对列的值进行分组[重复]