Q:

JSON.parse result to proper array of objects

after my JSON.parse I have this data
 data = [
  {
      "serviceType": "service1",
      "method": "close",
      "quantity": 56
  },
  {
      "serviceType": "service1",
      "method": "divert",
      "quantity": 175
  },
  {
      "serviceType": "service1",
      "method": "reverted",
      "quantity": 80
  },
  {
      "serviceType": "service2",
      "method": "close",
      "quantity": 15
  },
  {
      "serviceType": "service2",
      "method": "divert",
      "quantity": 149
  },
  {
      "serviceType": "service2",
      "method": "reverted",
      "quantity": 149
  },
  {
      "serviceType": "service3",
      "method": "close",
      "quantity": 87
  },....
  }
]

And I need it to be like data.json from this d3 example where categorie is my serviceType, being it the first property of every object (1 object per serviceType) and then the second property of every object should be an array of objects with the method and quantity.
I've been using forEach for every object in data and I'm pushing the first of every serviceType OK (without the 2nd property). The problem is I don't know how to update the object values (second property) inside the right object (according to the current serviceType).
finalArray = [];
data.forEach(function (d, index) {
    var serviceType = d.serviceType;    
    const found = finalArray.some( el => el.serviceType === serviceType);
    if (!found) {
        var obj = createObj(d); //function returning an obj with the desired structure
        finalArray.push(obj);                
    }...
});

This is an example of how the conversion should end up being.
[
    {
        "serviceType": "service1", 
        "values": [
            {
                "quantity": 0, 
                "method": "close"
            }, 
            {
                "quantity": 4, 
                "method": "divert"
            }, 
            {
                "quantity": 12, 
                "method": "reverted"
            }, 
            {
                "quantity": 0, 
                "method": "checked"
            }
        ]
    }, 
    {
        "serviceType": "service2", 
        "values": [
            {
                "quantity": 1, 
                "method": "close"
            }, 
            {
                "quantity": 21, 
                "method": "divert"
            }, 
            {
                "quantity": 13, 
                "method": "reverted"
            },  
            {
                "quantity": 6, 
                "method": "checked"
            }
        ]
    }, ...

A:

You could take aMap and group same serviceType. Later build an a array with categorie and values properties.

var data = [{ serviceType: "service1", method: "close", quantity: 56 }, { serviceType: "service1", method: "divert", quantity: 175 }, { serviceType: "service1", method: "reverted", quantity: 80 }, { serviceType: "service2", method: "close", quantity: 15 }, { serviceType: "service2", method: "divert", quantity: 149 }, { serviceType: "service2", method: "reverted", quantity: 149 }, { serviceType: "service3", method: "close", quantity: 87 }],
    result = Array.from(
        data.reduce((m, { serviceType, ...o }) => m.set(serviceType, [...(m.get(serviceType) ||  []), o]), new Map),
        ([categorie, values]) => ({ categorie, values })
    );

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

