go - Group slice by subarray values in Golang -


i have array of subarrays in following format

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., multiple or conflicting motivations) develop on course of                          )                  )          )      [1] => array         (             [unit_id] => 8584             [assignment_name] => sine , cosecant graphs             [assignment_description] => define sine , cosecant graphs using 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 , cosecant graphs                             [assignment_description] => define sine , cosecant graphs using 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                          )                  )          )  ) 

and group new slice based on unit_type_name field in each subarray.

how can group slice unit_type_name? there native go functions available this? if loop above duplicate one, how can avoid that?

i not think golang has build-in functionality (i may wrong). assuption php array converted json object. managed code below sort array (in json format) based on unit_type_name created 2 structs have json values similar array keys be

//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)     } 

i created map hold unit_type_name keys

    slicekeys := make(map[string]string) 

i created map hold arrays have similar unit_type_name keys in autogenerated array

    groupedslices := make(map[string][]autogenerated) 

then loop through decoded json string searching unit_type_name

    := range m { 

if unit_type_name exists in key slice add array item group slice

      if _, ok := slicekeys[m[i].unittypename]; ok {         autogenerated := groupedslices[m[i].unittypename]         autogenerated = append(autogenerated, m[i])         groupedslices[m[i].unittypename] = autogenerated       } else { 

else create new array key , add item it

        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) } 

input:

[{"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., multiple or conflicting motivations) develop on course of " }]},{"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., multiple or conflicting motivations) develop on course of "}]},{ "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., multiple or conflicting motivations) develop on course of "}]}] 

output:

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., multiple or conflicting motivations) develop on course of }]} ]   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., multiple or conflicting motivations) develop on course of }]} ]   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., multiple or conflicting motivations) develop on course of }]}] 

]


Comments

Popular posts from this blog

angular - Is it possible to get native element for formControl? -

unity3d - Rotate an object to face an opposite direction -

javascript - Why jQuery Select box change event is now working? -