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",...}[
]
1

5 Answers 5

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

Sign up to request clarification or add additional context in comments.

3 Comments

Why hash = Object.create(null), rather than hash={}
@mplungjan, it makes an empty object without prototypes, like toString (if that is a value for the hash, it could be true on check, but ist isn't backed with an array)
I see. That is a new one. It is a lot smaller than {}
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));

Comments

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

Comments

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.