Skip to content

Instantly share code, notes, and snippets.

@Sigmus
Created December 13, 2019 11:10
Show Gist options
  • Save Sigmus/3a9d5174174deaeab4639bd433f89bc5 to your computer and use it in GitHub Desktop.
Save Sigmus/3a9d5174174deaeab4639bd433f89bc5 to your computer and use it in GitHub Desktop.
const dayjs = require("dayjs");
const data = {
result: {
farmName: "Whitecreek Farm",
farmInfo: [
{
id: 1,
date: "22/02/2020",
type: "icon--reciept",
name: "ForFarmers Ltd",
additive: "Bovaer®",
kg: "5"
},
{
id: 2,
date: "08/02/2020",
type: "icon--reciept",
name: "ForFarmers Ltd",
additive: "Bovaer®",
kg: "2"
},
{
id: 3,
date: "17/01/2020",
type: "icon--download",
name: "ForFarmers Ltd",
additive: "Bovaer®",
kg: "10"
},
{
id: 4,
date: "08/01/2020",
type: "icon--reciept",
name: "Farming World TM",
additive: "Bovaer",
kg: "4"
},
{
id: 5,
date: "02/01/2020",
type: "icon--reciept",
name: "ForFarmers Ltd",
additive: "Bovaer®",
kg: "8"
},
{
id: 6,
date: "23/12/2019",
type: "icon--reciept",
name: "ForFarmers Ltd",
additive: "Bovaer®",
kg: "16"
},
{
id: 7,
date: "15/12/2019",
type: "icon--download",
name: "Farming World TM",
additive: "Bovaer®",
kg: "10"
},
{
id: 8,
date: "08/12/2019",
type: "icon--download",
name: "ForFarmers Ltd",
additive: "Bovaer®",
kg: "7"
},
{
id: 9,
date: "29/11/2019",
type: "icon--reciept",
name: "ForFarmers Ltd",
additive: "Bovaer®",
kg: "8"
},
{
id: 10,
date: "21/11/2019",
type: "icon--reciept",
name: "ForFarmers Ltd",
additive: "Bovaer®",
kg: "10"
},
{
id: 11,
date: "15/11/2019",
type: "icon--download",
name: "Farming World TM",
additive: "Bovaer®",
kg: "2"
},
{
id: 231,
date: "22/02/2020",
type: "icon--reciept",
name: "ForFarmers Ltd",
additive: "Bovaer®",
kg: "5"
},
{
id: 232,
date: "08/02/2020",
type: "icon--reciept",
name: "ForFarmers Ltd",
additive: "Bovaer®",
kg: "2"
},
{
id: 233,
date: "17/01/2020",
type: "icon--download",
name: "ForFarmers Ltd",
additive: "Bovaer®",
kg: "10"
},
{
id: 234,
date: "08/01/2020",
type: "icon--reciept",
name: "Farming World TM",
additive: "Bovaer",
kg: "4"
},
{
id: 235,
date: "02/01/2020",
type: "icon--reciept",
name: "ForFarmers Ltd",
additive: "Bovaer®",
kg: "8"
},
{
id: 236,
date: "23/12/2019",
type: "icon--reciept",
name: "ForFarmers Ltd",
additive: "Bovaer®",
kg: "16"
},
{
id: 9064781,
date: "22/02/2020",
type: "icon--reciept",
name: "ForFarmers Ltd",
additive: "Bovaer®",
kg: "5"
},
{
id: 9064782,
date: "08/02/2020",
type: "icon--reciept",
name: "ForFarmers Ltd",
additive: "Bovaer®",
kg: "2"
},
{
id: 9064783,
date: "17/01/2020",
type: "icon--download",
name: "ForFarmers Ltd",
additive: "Bovaer®",
kg: "10"
},
{
id: 9064784,
date: "08/01/2020",
type: "icon--reciept",
name: "Farming World TM",
additive: "Bovaer",
kg: "4"
},
{
id: 9064785,
date: "02/01/2020",
type: "icon--reciept",
name: "ForFarmers Ltd",
additive: "Bovaer®",
kg: "8"
},
{
id: 9064786,
date: "23/12/2019",
type: "icon--reciept",
name: "ForFarmers Ltd",
additive: "Bovaer®",
kg: "16"
}
]
}
};
const withDateObj = data.result.farmInfo.map(i => {
const [day, month, year] = i.date.split("/");
return { ...i, date: dayjs(`${year}-${month}-${day}`) };
});
const grouped = withDateObj.reduce((acc, next) => {
const key = next.date.format("YYYY-MM");
acc[key] = acc[key] || [];
acc[key].push(next);
return acc;
}, {});
// console.log(JSON.stringify(grouped, null, 4));
const months = Object.keys(grouped);
const result = months.map(month => {
console.log("start month:", month);
const items = grouped[month];
items.map(item => {
console.log(item);
});
return items;
});
// console.log(JSON.stringify(result, null, 4));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment