#Flat data to nested object transformation

7 messages · Page 1 of 1 (latest)

toxic path
#

What's the best way to transform flat data to a nested object grouped by different properties?

Our main concerns are about readability and maintainability.

Here is the data we have to transform :

const input = [
    {
        "ovenName": "Oven1",
        "productCode": "P2",
        "userId": 55,
        "userName": "user1",
        "quantity": 2
    },
    {
        "ovenName": "Oven1",
        "productCode": "P1",
        "userId": 55,
        "userName": "user1",
        "quantity": 15
    },
    {
        "ovenName": "Oven1",
        "productCode": "P3",
        "userId": 56,
        "userName": "user2",
        "quantity": 9
    },
    {
        "ovenName": "Oven2",
        "productCode": "P4",
        "userId": 56,
        "userName": "user2",
        "quantity": 88
    },
    {
        "ovenName": "Oven1",
        "productCode": "P1",
        "userId": 56,
        "userName": "user2",
        "quantity": 6
    }
];
const wantedOutput = [
    {
        "oven": "Oven1",
        "clients": [
            {
                "user":{
                    "name": "user1",
                    "id" : 55
                },
                "orders": [
                    {
                        "product": "P2",
                        "quantity": 2
                    },
                    {
                        "product": "P1",
                        "quantity": 15
                    }
                ]
            },
            {
                "user":{
                    "name": "user2",
                    "id" : 56
                },
                "orders": [
                    {
                        "product": "P3",
                        "quantity": 9
                    },
                    {
                        "product": "P1",
                        "quantity": 6
                    }
                ]
            }
        ]
    },
    {
        "oven": "Oven2",
        "clients": [
            {
                "user":{
                    "name": "user2",
                    "id" : 56
                },
                "orders": [
                    {
                        "product": "P4",
                        "quantity": 88
                    }
                ]
            }
        ]
    }
]

Our input is flat and we want to group by oven then by users and then by order (each product have a different order).
We already have a solution but we would like input on what direction to approach this issue (our solution is unreadable and we are looking for a better way)

teal heron
#

Object.groupBy might be useful if you're targeting newer runtimes (you can polyfill it for older ones too)

#

naive usage (Object.groupBy(input, value => value.ovenName)) will produce a slightly different structure from what you asked for:

{
  Oven1: [
    {
      ovenName: 'Oven1',
      productCode: 'P2',
      userId: 55,
      userName: 'user1',
      quantity: 2,
    },
    {
      ovenName: 'Oven1',
      productCode: 'P1',
      userId: 55,
      userName: 'user1',
      quantity: 15,
    },
    {
      ovenName: 'Oven1',
      productCode: 'P3',
      userId: 56,
      userName: 'user2',
      quantity: 9,
    },
    {
      ovenName: 'Oven1',
      productCode: 'P1',
      userId: 56,
      userName: 'user2',
      quantity: 6,
    },
  ],
  Oven2: [
    {
      ovenName: 'Oven2',
      productCode: 'P4',
      userId: 56,
      userName: 'user2',
      quantity: 88,
    },
  ],
}
#

but perhaps that's good enough for what you need?

#

if not i'd probably use .reduce to build the result myself

#

oh wait, i just noticed you want to group at two levels

#

i guess i would reach for .reduce