Group by key + merge array values

inalgnu :

I'm trying to merge the array values of multiple arrays with same keys (sort of group by + merge). Is there any native way in Go to transform this input:

input = [
    [
        { a: 1, b: [1,2]},
        { a: 2, b: [1,2]},
        { a: 3, b: [1,2]}
    ],
    [
        { a: 1, b: [3,4]},
        { a: 2, b: [3,4]},
        { a: 3, b: [3,4]},
        { a: 4, b: [3,4]}
    ],
    [
        { a:1, b: [5,6]},
        { a:2, b: [5,6]},
        { a:3, b: [5,6]},
        { a:4, b: [5,6]},
        { a:5, b: [5,6]}
    ]
 ]

into:

output = [
    { a: 1, b: [1,2,3,4,5,6]},
    { a: 2, b: [1,2,3,4,5,6]},
    { a: 3, b: [1,2,3,4,5,6]},
    { a: 4, b: [3,4, 5,6]},
    { a: 5, b: [5,6]},
]
Darshan Rivka Whittle :

Your code isn't Go code (it would pass as Ruby), which, along with no clear attempt on your part to actually solve the problem, is probably why you got some downvotes and no answers yet.

One approach would be to interpret your code as Ruby would, with input a slice of slices of maps of (string, maybe, although Ruby would treat them as Symbols) keys to either ints (in the case of a) or slices of ints (in the case of b). You could do that with the map values using the empty interface (interface{}), but it would be more straightforward to use a struct like this:

type ab struct {
    a int
    b []int
}

and have input be a [][]ab. But as long as we're restructuring your data to fit the language better, it's even more straightforward to do away with a and b altogether, replacing that structure with a map where the a values are the keys and the b values are the values. Things are then a simple matter of iterating over input and appending slices into output:

package main

import "fmt"

func main() {
    input := []map[int][]int{
        {
            1: []int{1, 2},
            2: []int{1, 2},
            3: []int{1, 2},
        },
        {
            1: []int{3, 4},
            2: []int{3, 4},
            3: []int{3, 4},
            4: []int{3, 4},
        },
        {
            1: []int{5, 6},
            2: []int{5, 6},
            3: []int{5, 6},
            4: []int{5, 6},
            5: []int{5, 6},
        },
    }

    output := make(map[int][]int)

    for _, m := range input {
        for k, v := range m {
            output[k] = append(output[k], v...)
        }
    }

    fmt.Println(output)
}

This produces the output:

map[1:[1 2 3 4 5 6] 2:[1 2 3 4 5 6] 3:[1 2 3 4 5 6] 4:[3 4 5 6] 5:[5 6]]

output thus has the data organized as you wanted, given the modified way of representing that data organization.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

merge values of same key in an array to values array

Group array values by string in key?

Python: Group Unique ids and Merge Values into an Array

Group by array with key with another array of values

Array of objects find key name and merge the values

Merge multidimensional array values by shared key

Merge objects in an array based on key values

Lodash merge array with dictionary object key values

Merge array by index/key keeping all values

Array merge or push key and values of one array into another array

Group Array of Hashes by Key followed by values

Group multidimensional array by key and sum values in PHP

Group array values based on key in Lua?

Python - group/merge dictionaries based on key/values identity

Merge array depending key values in a multidimensional array in PHP

Merge a simple array of values into an array of objects by adding a key

PHP array_merge that ignores key values that are not in the first/base array

ruby array of hash merge array values based on key

Typescript/Lodash group by -- Trying to group values of a key of an array with their quantity

Group/merge array values based on a set number range

Merge/group array values to stop duplicated parents with exactly the same fullname

How to merge hashes if a specified key's values are same in a array

How to merge/combine two values into single key in the same array

Merge two array of objects into one by given key and duplicating other values

Swift: Merge Array of dictionaries with duplicate key but unique values

Merge values to an array based on similar object key javascript

Merge values for same key

Merge Group Array in PHP

Multiple version of keyBy in lodash? (Group values sharing a key as an array)