在Golang中按子阵列值分组

时间:2016-09-22 11:55:29

标签: go slice

我有以下格式的子数组

var status = dbContext.Status.ToList();
var role = dbContext.UserRole.ToList();
var testuser = dbContext.User.Where(u => u.CompanyID == 1).FirstOrDefault();

我想根据每个子阵列中的var status = dbContext.Status.ToList(); 字段将其分组到一个新的切片中。

如何按Array ( [0] => Array ( [unit_id] => 6504 [assignment_name] => Grade assignment [assignment_description] => [assignment_total_score] => 10 [unit_type_name] => Homework [is_graded] => 1 [standard_id] => 1219 [scoring_type] => score [attempt_score] => 8 [unit_duedate] => 2016-02-10 09:00:00 [standard] => Array ( [0] => stdClass Object ( [unit_id] => 6504 [is_formal] => 1 [assignment_name] => Grade assignment [assignment_description] => [standard_id] => 1220 [standard_name] => 9-10.RL.3 [standard_description] => Analyze how complex characters (e.g., those with multiple or conflicting motivations) develop over the course of a ) ) ) [1] => Array ( [unit_id] => 8584 [assignment_name] => Sine and Cosecant Graphs [assignment_description] => Define the sine and cosecant graphs using a unit circle [assignment_total_score] => 15 [unit_type_name] => Paper [scoring_type] => score [attempt_score] => 0 [unit_duedate] => 2016-04-29 09:00:00 [standard] => Array ( [0] => stdClass Object ( [unit_id] => 8584 [is_formal] => 1 [assignment_name] => Sine and Cosecant Graphs [assignment_description] => Define the sine and cosecant graphs using a unit circle [assignment_total_score] => 15 [standard_id] => 82790 [standard_name] => 9-10.RL.7 ) ) [2] => Array ( [unit_id] => 11611 [assignment_name] => Adding 5 + 3 + 6 [assignment_description] => [assignment_total_score] => 10 [unit_type_name] => Homework [standard_id] => 82772 [scoring_type] => score [attempt_score] => 0 [unit_duedate] => 2016-08-23 19:00:00 [standard] => Array ( [0] => stdClass Object ( [unit_id] => 11611 [is_formal] => 1 [assignment_name] => Adding 5 + 3 + 6 [assignment_description] => [assignment_total_score] => 10 [standard_id] => 82772 [standard_name] => 9-10.RL.1 ) ) ) ) 对切片进行分组?是否有任何本机GO功能可用于执行此操作? 如果我循环上面然后我会得到一个重复的,我怎么能避免这个?

1 个答案:

答案 0 :(得分:1)

我认为golang没有内置功能来帮助你做到这一点(我可能错了)。我的Assuption是php数组将被转换为json对象。我设法得到以下代码,以帮助您根据unit_type_name对数组(JSON格式)进行排序 我创建了两个结构,其json值类似于数组键

//StandardType ...
type StandardType struct {
    UnitID                int    `json:"unit_id"`
    IsFormal              int    `json:"is_formal"`
    AssignmentName        string `json:"assignment_name"`
    AssignmentDescription string `json:"assignment_description"`
    StandardID            int    `json:"standard_id"`
    StandardName          string `json:"standard_name"`
    StandardDescription   string `json:"standard_description"`
}

//AutoGenerated ...
type AutoGenerated struct {
    UnitID                int            `json:"unit_id"`
    AssignmentName        string         `json:"assignment_name"`
    AssignmentDescription string         `json:"assignment_description"`
    AssignmentTotalScore  int            `json:"assignment_total_score"`
    UnitTypeName          string         `json:"unit_type_name"`
    IsGraded              int            `json:"is_graded"`
    StandardID            int            `json:"standard_id"`
    ScoringType           string         `json:"scoring_type"`
    AttemptScore          int            `json:"attempt_score"`
    UnitDuedate           string         `json:"unit_duedate"`
    Standard              []StandardType `json:"standard"`
}
var jsonData = ``
func main() { 
    m := []AutoGenerated{}
    err := json.Unmarshal([]byte(jsonData), &m)
    if err != nil {
        panic(err)
    }

我创建了一个地图来保存unit_type_name

    sliceKeys := make(map[string]string)

我还创建了map来保存AutoGenerated数组中具有类似unit_type_name键的数组

    groupedSlices := make(map[string][]AutoGenerated)

然后我遍历解码的json字符串,搜索unit_type_name

    for i := range m {

如果密钥切片中已存在unit_type_name,我将数组项添加到组切片

      if _, ok := sliceKeys[m[i].UnitTypeName]; ok {
        autogenerated := groupedSlices[m[i].UnitTypeName]
        autogenerated = append(autogenerated, m[i])
        groupedSlices[m[i].UnitTypeName] = autogenerated
      } else {

否则我创建一个新的数组键并将项添加到其中

        sliceKeys[m[i].UnitTypeName] = m[i].UnitTypeName
        autogenerated := []AutoGenerated{}
        autogenerated = append(autogenerated, m[i])
        groupedSlices[m[i].UnitTypeName] = autogenerated
     }
  }
  fmt.Println(sliceKeys)
  fmt.Println(groupedSlices)
}

输入:

[{"unit_id": 6504,"assignment_name": "Grade assignment","assignment_description": "","assignment_total_score": 10,"unit_type_name": "Homework","is_graded": 1,"standard_id": 1219,
"scoring_type": "score","attempt_score": 8,"unit_duedate": "2016-02-10 09:00:00",
"standard": [{"unit_id": 6504,"is_formal": 1,"assignment_name": "Grade assignment","assignment_description": "",
"standard_id": 1220,"standard_name": "9-10.RL.3","standard_description": "Analyze how complex characters (e.g., those with multiple or conflicting motivations) develop over the course of a "
}]},{"unit_id": 6504,"assignment_name": "Grade assignment","assignment_description": "","assignment_total_score": 10,
"unit_type_name": "Paper","is_graded": 1,"standard_id": 1219,"scoring_type": "score","attempt_score": 8,"unit_duedate": "2016-02-10 09:00:00","standard": [{"unit_id": 6504,"is_formal": 1,"assignment_name": "Grade assignment","assignment_description": "","standard_id": 1220,"standard_name": "9-10.RL.3","standard_description": "Analyze how complex characters (e.g., those with multiple or conflicting motivations) develop over the course of a "}]},{
"unit_id": 6504,"assignment_name": "Grade assignment","assignment_description": "",
"assignment_total_score": 10,"unit_type_name": "Aything else","is_graded": 1,"standard_id": 1219,
"scoring_type": "score","attempt_score": 8,"unit_duedate": "2016-02-10 09:00:00","standard": [{
"unit_id": 6504,"is_formal": 1,"assignment_name": "Grade assignment","assignment_description": "","standard_id": 1220,
"standard_name": "9-10.RL.3","standard_description": "Analyze how complex characters (e.g., those with multiple or conflicting motivations) develop over the course of a "}]}]

输出:

map[Homework:Homework Paper:Paper Aything else:Aything else]

map[
Homework:[
{6504 Grade assignment  10 Homework 1 1219 score 8 2016-02-10 09:00:00 [{6504 1 Grade assignment  1220 9-10.RL.3 Analyze how complex characters (e.g., those with multiple or conflicting motivations) develop over the course of a }]}
] 

Paper:[
{6504 Grade assignment  10 Paper 1 1219 score 8 2016-02-10 09:00:00 [{6504 1 Grade assignment  1220 9-10.RL.3 Analyze how complex characters (e.g., those with multiple or conflicting motivations) develop over the course of a }]}
] 

Aything else:[
{6504 Grade assignment  10 Aything else 1 1219 score 8 2016-02-10 09:00:00 [{6504 1 Grade assignment  1220 9-10.RL.3 Analyze how complex characters (e.g., those with multiple or conflicting motivations) develop over the course of a }]}]

相关问题