【问题标题】:golang filter based on key in array of map interfaces基于地图接口数组中的键的golang过滤器
【发布时间】:2018-02-23 13:54:14
【问题描述】:

作为学习的一部分,我尝试了以下代码 sn-ps。

func ASNGroup(asn []map[string]interface{}) bool {
    eachgroup := make([][]map[string]interface{}, 0)

    for i := range asn {
        for _, v := range asn[i] {
            // How to filter the based on Key based on the below filter i should group the above asn array maps.
                    asn[i]["ID"]    
        }
    }
    fmt.Println(eachgroup)

    return true
}

请帮助我编写代码,是的,我避免使用 struct,因为我正在根据上传的 xlsx 表准备 asn 对象。是的,我知道这是强制密钥,因此我可以硬编码此密钥以进行过滤。我知道它不像javascript那么容易。对于编写函数,我知道应该有一些返回初始化,我初始化了 dummy bool 以避免错误。

请不要偏离问题,提出建议

理解并请帮助在逻辑上将这样的 [[],[]] 分组。

这是 []map[string]interface{}

的以下示例
   [{"id":"1","seperator":"B","code":"twenty1"},
   {"id":"2","seperator":"A","code":"twenty2"},
   {"id":"3","seperator":"B","code":"twenty3"}]

分隔符是对象内部分隔对象的关键。

{"B" : [{"id":"1","seperator":"B","code":"twenty1"},
{"id":"3","seperator":"B","code":"twenty3"}]
, "A" :  [{"id":"2","seperator":"A","code":"twenty2"}]}

【问题讨论】:

  • 你能举一个你想要的输出的例子吗?
  • 当然@maerics,我正在更新上述问题本身。请检查一下。

标签: go interface


【解决方案1】:

听起来您想对标识的键具有相同值的所有项目进行分组,通常称为“分组依据”操作(例如,在 JavaScript 中,请参见 _.groupBy(...))..

要实现“分组依据”,您只需遍历给定的集合,查找目标键的值,并将当前对象附加到与该键的值对应的结果数组中。

例如:

func groupBy(maps []map[string]interface{}, key string) map[string][]map[string]interface{} {
  groups := make(map[string][]map[string]interface{})
  for _, m := range maps {
    k := m[key].(string) // XXX: will panic if m[key] is not a string.
    groups[k] = append(groups[k], m)
  }
  return groups
}

func main() {
  xs := []map[string]interface{}{
    {"id": "1", "seperator": "B", "code": "twenty1"},
    {"id": "2", "seperator": "A", "code": "twenty2"},
    {"id": "3", "seperator": "B", "code": "twenty3"},
  }
  grouped := groupBy(xs, "seperator")
  // {
  //   "A": [
  //     { "code": "twenty2", "id": "2", "seperator": "A" }
  //   ],
  //   "B": [
  //     { "code": "twenty1", "id": "1", "seperator": "B" },
  //     { "code": "twenty3", "id": "3", "seperator": "B" }
  //   ]
  // }
}

请注意,为避免上述示例代码中可能出现的“恐慌”,您应该使用双值类型断言表单并修改您的函数签名以返回两个值(映射和错误):

func groupBy(...) (map..., error) {
  //...
    k, ok := m[key].(string)
    if !ok {
      return nil, fmt.Errorf("expected string value type for key %q, got %T", key, m[key])
    }
  // ...
  return groups, nil
}

【讨论】:

    【解决方案2】:

    您可以使用我创建的 https://github.com/ledongthuc/goterators#group 中的 Group() 来重用聚合和转换函数。

        groups := goterators.Group(asn, func(item map[string]interface{}) string {
            return item["seperator"].(string)
        })
    
    

    它将根据您定义的标准将元素分组到列表的 1 个嵌套级别。

    此库需要 Go 1.18 才能使用支持您要使用的泛型 + 动态类型。

    【讨论】:

      猜你喜欢
      • 2015-04-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-13
      • 2017-09-08
      • 1970-01-01
      相关资源
      最近更新 更多