0

I'm using Typescript with Lodash!

After I get my data from database i have this output:

[
  {
    "unitPrice": 0.01,
    "code": "92365524",
    "description": "Broto gratis (chocolate)",
    "menu": {
      "code": "1DFD0",
      "name": "Promoção"
    }
  },
  {
    "unitPrice": 0,
    "code": "060d6fcf1c0d3663a1747dd249afa4db",
    "description": "BROTO",
    "menu": {
      "code": "1CZSR",
      "name": "Cardápio americano (broto)"
    }
  },
  {
    "unitPrice": 0,
    "code": "ac91957034d03e818968b46663a61826",
    "description": "BROTO",
    "menu": {
      "code": "1CZSR",
      "name": "Cardápio americano (broto)"
    }
  }
]

I want to group by Menu object, i tryied a lot and i'm not close to get it. I don't know how to do this sort. I'm using lodas, but do i need to use another lib to do this?

This is what a i need for the response of my endpoint, it looks easy to do, but i can't figure it out:

{
  "menu": [
    {
      "code": "1CZSR",
      "name": "Cardápio americano (broto)",
      "itens": [
        {
          "unitPrice": 0.01,
          "code": "92365524",
          "description": "Broto gratis (chocolate)"
        }
      ]
    },
    {
      "code": "1DFD0",
      "name": "Promoção",
      "itens": [
        {
          "unitPrice": 0,
          "code": "060d6fcf1c0d3663a1747dd249afa4db",
          "description": "BROTO",
        },
        {
          "unitPrice": 0,
          "code": "ac91957034d03e818968b46663a61826",
          "description": "BROTO",
        }
      ]
    }
  ]
}

After try a lot, this is the code i have the closest i could to de response I want.

const data = _.mapValues(_.groupBy(results, 'menu.code'), (menuItem) =>
      menuItem.map((item) => _.omit(item, 'menu')),
    );

but the output is:

{
  "1CZSR":[
    {
      "unitPrice": 0.01,
      "code": "92365524",
      "description": "Broto gratis (chocolate)"
    }
  ],
  "1DFD0": [
    {
      "unitPrice": 0,
      "code": "060d6fcf1c0d3663a1747dd249afa4db",
      "description": "BROTO",
    },
    {
      "unitPrice": 0,
      "code": "ac91957034d03e818968b46663a61826",
      "description": "BROTO",
    }
  ]
}

What did i forget?

Gabriel Lemos
  • 65
  • 1
  • 9
  • Does this answer your question? [How to group an array of objects by key](https://stackoverflow.com/questions/40774697/how-to-group-an-array-of-objects-by-key) – pilchard Dec 20 '20 at 21:28

1 Answers1

2

This is a fairly easy groupBy using vanilla js with a bit of destructuring

const res = Object.values(
    data.reduce((a, {menu: {code, name}, ...rest}) => {
       a[code] = a[code] || {code, name, items: []};
       return  a[code].items.push({...rest}), a;      
    },{})
);

console.log(res)
.as-console-wrapper {max-height: 100%!important;top:0;}
<script>
const data=[{unitPrice:.01,code:"92365524",description:"Broto gratis (chocolate)",menu:{code:"1DFD0",name:"Promoção"}},{unitPrice:0,code:"060d6fcf1c0d3663a1747dd249afa4db",description:"BROTO",menu:{code:"1CZSR",name:"Cardápio americano (broto)"}},{unitPrice:0,code:"ac91957034d03e818968b46663a61826",description:"BROTO",menu:{code:"1CZSR",name:"Cardápio americano (broto)"}}];
</script>
charlietfl
  • 170,828
  • 13
  • 121
  • 150