0

I have array of objects as follow:

[
    {
        "category": "Drinks",
        "id": 1,
        "name": "COLA",
        "price": 0,
        "type": 1
    },
    {
        "category": "Drinks",
        "id": 2,
        "name": "COFFEE",
        "price": 2.5,
        "type": 1
    },
    {
        "category": "Addons",
        "id": 3,
        "name": "Extra cheese",
        "price": 5,
        "type": 0
    },
    {
        "category": "Addons",
        "id": 4,
        "name": "Extra vagg",
        "price": 3,
        "type": 0
    },

]

I want to filter the data based on category name and type so the data that have same category name and same type number become together, the result be something like

[
   Drinks:[
            {
              "id": 1,
              "name": "COLA",
              "price": 0,
              "type": 1
            },
            {
             "id": 2,
             "name": "COFFEE",
             "price": 2.5,
             "type": 1
            }
          ],
    Addons:[
            {
             "id": 3,
             "name": "Extra cheese",
             "price": 5,
             "type": 0
            },
            {
             "id": 4,
             "name": "Extra vagg",
             "price": 3,
             "type": 0
            }
           ]
  ]

note that category names not always the same names as above. Hope someone can help, thanks.

anie
  • 399
  • 5
  • 17
  • Please visit [help], take [tour] to see what and [ask]. Do some research, [search for related topics on SO](https://www.google.com/search?q=Filter+OR+group+array+of+objects+based+on+conditions+site%3Astackoverflow.com); if you get stuck, post a [mcve] of your attempt, noting input and expected output, preferably in a [Stacksnippet](https://blog.stackoverflow.com/2014/09/introducing-runnable-javascript-css-and-html-code-snippets/) – mplungjan Jul 01 '21 at 11:26
  • There is nothing React about filtering or grouping – mplungjan Jul 01 '21 at 11:26

0 Answers0