Question: I have an array of objects. These objects have a property called `time`. I want to group these objects to the same array if their time are same.

```
{ "00:00" :
   [{"id":1, time: "00:05",
    {"id":1, time: "00:15",
    {"id":1, time: "00:20",
    {"id":2, time: "00:05",
    {"id":3, time: "00:05",
    {"id":4, time: "00:35 }]
}

```

I want to format the above data as:

```
{ "00:00" :[
    [{"id":1, time: "00:05"}, {"id":2, time: "00:05"}, {"id":3, time: "00:05"}],
    [{"id":1, time: "00:15" }],
    [{"id":1, time: "00:20" }],      
    [{"id":4, time: "00:35" }]]
}

```

Any advice on how can I accomplish this?

Answer: 
Use `.reduce` to put all matching objects into an array inside an object indexed by the time, and then extract the values from the object:

```js
const input = { "00:00" :
   [{"id":1, time: "00:05"},
    {"id":1, time: "00:15"},
    {"id":1, time: "00:20"},
    {"id":2, time: "00:05"},
    {"id":3, time: "00:05"},
    {"id":4, time: "00:35" }]
};
const outputObj = input['00:00'].reduce((accum, { id, time }) => {
  if (!accum[time]) accum[time] = [];
  accum[time].push({ id, time });
  return accum;
}, {});
const outputArr = Object.values(outputObj);
console.log(outputArr);
```

You can also extend it to apply to multiple properties of the `input` array:

```js
const input = { "00:00" :
   [{"id":1, time: "00:05"},
    {"id":1, time: "00:15"},
    {"id":1, time: "00:20"},
    {"id":2, time: "00:05"},
    {"id":3, time: "00:05"},
    {"id":4, time: "00:35" }],
    "01:00" :
   [{"id":1, time: "01:05"},
    {"id":1, time: "01:15"},
    {"id":1, time: "01:20"},
    {"id":2, time: "01:05"},
    {"id":3, time: "01:05"},
    {"id":4, time: "01:35" }],
};
function transformInputArrToGroupedArr(inputArr) {
  const outputObj = inputArr.reduce((accum, { id, time }) => {
    if (!accum[time]) accum[time] = [];
    accum[time].push({ id, time });
    return accum;
  }, {});
  return Object.values(outputObj);
}
const transformedInput = Object.entries(input).reduce((accum, [ key, arr ]) => {
  accum[key] = transformInputArrToGroupedArr(arr);
  return accum;
}, {});
console.log(transformedInput);
```
You can try with Array's [`reduce()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce) like the following way:

```js
var obj = { "00:00" :
   [{"id":1, time: "00:05"},
    {"id":1, time: "00:15"},
    {"id":1, time: "00:20"},
    {"id":2, time: "00:05"},
    {"id":3, time: "00:05"},
    {"id":4, time: "00:35" }]
}

var res = obj['00:00'].reduce(function(a, c){
  a[c.time] = a[c.time] || [];
  a[c.time].push(c);
  return a;
}, {});

console.log(res);
```

```css
.as-console-wrapper { top: 0; }
```