-1

I got a json file that contains beer types like this:

  {
    "type": "Unifiltered",
    "name": "Heineken Unfiltered",
    "id": "XY",
    "brand": "Heineken",
    "price": "1250",
    "alcohol": "0.04",
    "ingredients": [
      {
        "id": "XY2",
        "ratio": "0.15",
        "name": "salt"
      },
      {
        "id": "XY3",
        "ratio": "0.00",
        "name": "sugar"
      },
      {
        "id": "XY4",
        "ratio": "0.35",
        "name": "barley"
      }
    ],
    "isCan": false
  },

My task is to group beers by brand:

My friend has a list of all the beers available in his pub, but it’s a huge mess. He would like to see the beers grouped by brands. My friend also told you that the Function should return an array of Brand > objects which contain the array of Beers of that Brand.

Example:

[{brand: Heineken, beers: [{...}, ...]}]"
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • reduce() and Object.values Sounds like a homework so not going to give you the solution. Have fun. – epascarello Jan 25 '21 at 23:52
  • Welcome to StackOverflow. Your original question topic said you are interested in programming. Asking for someone to solve the task makes it look like you are not interested at all Code something up - if you can't get it to work, come back and ask specific questions. – Randy Casburn Jan 25 '21 at 23:53
  • Do you have any idea where to start? If so please [edit] your question and tell us your ideas and show us any code you have. If not I don't think Stack Overflow is a good place to ask. We can help you put the final touches on code you've already written but we can't teach you everything from ground zero. – John Kugelman Jan 25 '21 at 23:53
  • Yes it is like a homework but if you help me with this one i can do the next tasks but i dont know how to start – Falco_99 Jan 25 '21 at 23:57
  • 1
    Does this answer your question? [How to groupBy array of objects based on properties in vanilla javascript](https://stackoverflow.com/questions/57522552/how-to-groupby-array-of-objects-based-on-properties-in-vanilla-javascript) – Garrett Motzner Jan 25 '21 at 23:58

3 Answers3

2

const beers = [{
    "type": "Unifiltered",
    "name": "Heineken Unfiltered",
    "id": "XY",
    "brand": "Heineken",
    "price": "1250",
    "alcohol": "0.04",
    "ingredients": [
      {
        "id": "XY2",
        "ratio": "0.15",
        "name": "salt"
      },
      {
        "id": "XY3",
        "ratio": "0.00",
        "name": "sugar"
      },
      {
        "id": "XY4",
        "ratio": "0.35",
        "name": "barley"
      }
    ],
    "isCan": false
  },
{
    "type": "type",
    "name": "name",
    "id": "XY",
    "brand": "EFES",
    "price": "1250",
    "alcohol": "0.04",
    "ingredients": [
      {
        "id": "XY2",
        "ratio": "0.15",
        "name": "salt"
      },
      {
        "id": "XY3",
        "ratio": "0.00",
        "name": "sugar"
      },
      {
        "id": "XY4",
        "ratio": "0.35",
        "name": "barley"
      }
    ],
    "isCan": false
  },
{
    "type": "type3",
    "name": "name2",
    "id": "XY",
    "brand": "EFES",
    "price": "1250",
    "alcohol": "0.04",
    "ingredients": [
      {
        "id": "XY2",
        "ratio": "0.15",
        "name": "salt"
      },
      {
        "id": "XY3",
        "ratio": "0.00",
        "name": "sugar"
      },
      {
        "id": "XY4",
        "ratio": "0.35",
        "name": "barley"
      }
    ],
    "isCan": false
  }];

var group = beers.reduce((r, a) => {
 r[a. brand] = [...r[a. brand] || [], a];
 return r;
}, {});

console.log("group", group);

i think its already answered here

let group = beers.reduce((r, a) => {
 r[a. brand] = [...r[a. brand] || [], a];
 return r;
}, {});
console.log("group", group);
menethil
  • 83
  • 5
0

Written in an easily readable format, using array.forEach() in place of shorthand functions:

let beers = [{
  "type": "Unifiltered",
  "name": "Heineken Unfiltered",
  "id": "XY",
  "brand": "Heineken",
  "price": "1250",
  "alcohol": "0.04",
  "ingredients": [
    {
      "id": "XY2",
      "ratio": "0.15",
      "name": "salt"
    },
    {
      "id": "XY3",
      "ratio": "0.00",
      "name": "sugar"
    },
    {
      "id": "XY4",
      "ratio": "0.35",
      "name": "barley"
    }
  ],
  "isCan": false
}];

let brands = [];

// Loop over all beers
beers.forEach((beer) => {
  // Try to find beer brand in existing list of brand
  let brand = brands.filter((brand) => brand.brand === beer.brand)[0];
  
  if(brand) {
    // If we find it, push the beer onto the beer property
    brand.beers.push(beer);
  } else {
    // If we don't find it, create a new brand and push it onto brands
    brand = {
      brand: beer.brand,
      beers: [beer]
    }
    brands.push(brand);
  }
});


console.log(brands);
Joundill
  • 6,828
  • 12
  • 36
  • 50
0

I think Grouping JSON by values can be useful. Using the group_by function and then its is just cleaning up the answer to get what you need.