Skip to content

Instantly share code, notes, and snippets.

@anaarezo
Created January 31, 2022 20:22
Show Gist options
  • Save anaarezo/6e0766703f1b79b293dd8aea17ba8089 to your computer and use it in GitHub Desktop.
Save anaarezo/6e0766703f1b79b293dd8aea17ba8089 to your computer and use it in GitHub Desktop.
Group array of objects based in Date
const data = [
{ transactionStatus: 'PROCESSING', date: '2021-12-26T09:40:50', brand: 'MASTERCARD', transactionType: 'CREDIT', value: 500000,},
{ transactionStatus: 'RECEIVED', date: '2021-12-26T10:12:41', brand: 'MASTERCARD', transactionType: 'CREDIT', value: 500000,},
{ transactionStatus: 'CANCELED', date: '2021-12-26T10:12:41', brand: 'MASTERCARD', transactionType: 'CREDIT', value: 500000,},
{ transactionStatus: 'CANCELED', date: '2021-12-27T12:35:23', brand: 'MASTERCARD', transactionType: 'CREDIT', value: 500000,},
];
const groups = data.reduce((groups, sale) => {
const time = sale.date.split('T')[0];
if (!groups[time]) {
groups[time] = [];
}
groups[time].push(sale);
return groups;
}, {});
const groupArrays = Object.keys(groups).map((time) => {
return {
time,
sales: groups[time]
};
});
console.log(groupArrays);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment