Last active
June 28, 2024 10:22
-
-
Save anaarezo/6e0766703f1b79b293dd8aea17ba8089 to your computer and use it in GitHub Desktop.
Group array of objects based in Date
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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