4

I hate this array of objects, each object has a date, I want to be able to group these objects into months. Is there a way to convert this,

var data  = [
  { date: "2016-08-13",...},
  { date: "2016-07-23",...},
  { date: "2016-08-11",...},
  { date: "2016-08-10",...},
  { date: "2016-07-20",...},
  { date: "2016-07-21",...},
]

into something like this

var data  = [
  [{ date: "2016-08-13",...},
  { date: "2016-08-11",...},
  { date: "2016-08-10",...}],
  [{ date: "2016-07-20",...},
  { date: "2016-07-21",...},
  { date: "2016-07-23",...}[
]
relidon
  • 2,142
  • 4
  • 21
  • 37
  • Possible duplicate of [What is the most efficient method to groupby on a javascript array of objects?](https://stackoverflow.com/questions/14446511/what-is-the-most-efficient-method-to-groupby-on-a-javascript-array-of-objects) – Andreas Nov 14 '17 at 07:44

5 Answers5

5

You could take a part of the string for year and month group in a hash table and take for every group a new array and put this array to the result set.

var data = [{ date: "2016-08-13" }, { date: "2016-07-23" }, { date: "2016-08-11" }, { date: "2016-08-10" }, { date: "2016-07-20" }, { date: "2016-07-21" }],
    hash = Object.create(null),
    result = [];

data.forEach(function (o) {
    var key = o.date.slice(0, 7);
    if (!hash[key]) {
        hash[key] = [];
        result.push(hash[key]);
    }
    hash[key].push(o);
});

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Redu
  • 25,060
  • 6
  • 56
  • 76
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
2

You can use array#reduce to group object based on month.

var data  = [{ date: "2016-08-13"},{ date: "2016-07-23"},{ date: "2016-08-11"},{ date: "2016-08-10"},{ date: "2016-07-20"},{ date: "2016-07-21"}];

var result = data.reduce((res,obj) => {
  let [year, month, day] = obj.date.split('-');
  if(res[month])
    res[month].push(obj);
  else
    res[month] = [obj];
  return res;
},{});
console.log(Object.values(result));
.as-console-wrapper { max-height: 100% !important; top: 0; }

var data  = [{ date: "2016-08-13"},{ date: "2016-07-23"},{ date: "2016-08-11"},{ date: "2016-08-10"},{ date: "2016-07-20"},{ date: "2016-07-21"}];

var result = data.reduce((res,obj) => {
  let [year, month, day] = obj.date.split('-');
  res[month] = res[month] || [];
  res[month].push(obj);
  return res;
},{});
console.log(Object.values(result));
Hassan Imam
  • 21,956
  • 5
  • 41
  • 51
0

var data  = [
  { date: "2016-08-13"},
  { date: "2016-07-23"},
  { date: "2016-08-11"},
  { date: "2016-08-10"},
  { date: "2016-07-20"},
  { date: "2016-07-21"},
];

data.sort(function(a, b) {
  var aDate = new Date(a.date);
  var bDate = new Date(b.date);
  return bDate - aDate;
});

console.log(data);
pmaddi
  • 449
  • 5
  • 18
0

Group by Month:

var groupedData = [];

for(i=0;i<12;i++)
   groupedData.push([]);

for(var index = 0;index<data.length;index++){
   var date = new Date(data[index].date);
   groupedData[date.getMonth()] = data[index];
}
Naveen
  • 75
  • 2
  • 9
0
var data  = [
{ date: "2016-08-13"},
{ date: "2016-07-23"},
{ date: "2016-08-11"},
{ date: "2016-08-10"},
{ date: "2016-07-20"},
{ date: "2016-07-21"}
];

var monthDateIndex = {
 "01": "Jan",
 "02" : "Feb",
 "03" : "mar",
 "04" : "april",
 "05" : "may",
 "06" : "june",
 "07" : "july",
 "08" : "august",
 "09" : "sept",
 "10" : "oct",
 "11" : "nov",
 "12" : "dec"
}

let newDataObj = [];
Object.keys(monthDateIndex).map(monthNum => {
        let monthObj = [];
   data.map(dateObj => {   
       if(monthNum == dateObj.date.split("-")[1]) {
        monthObj.push(dateObj);
     }
   });
   if(monthObj.length != 0) newDataObj.push(monthObj);
});
Rajkumar Somasundaram
  • 1,225
  • 2
  • 10
  • 20