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?