Skip to content Skip to sidebar Skip to footer

Group Array Of Objects By String Property Value In Javascript?

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',..

Solution 1:

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; }

Solution 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));

Solution 3:

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];
}

Solution 4:

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 = newDate(a.date);
  var bDate = newDate(b.date);
  return bDate - aDate;
});

console.log(data);

Solution 5:

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);
});

Post a Comment for "Group Array Of Objects By String Property Value In Javascript?"