'Using graph QL and React, how can I divide API by month?
For example, When there are a lot of data like this:
[
{title:"string" , data:"2022-05-01", amount:10},
{title:"string" , data:"2022-04-01", amount:10},
{title:"string" , data:"2022-03-03", amount:10},
{title:"string" , data:"2022-04-01", amount:10}
]
Is there a way to efficiently divide and manage the data every month? I want to make a monthly list using a filter and manage it. (sorry for my bad English)
Solution 1:[1]
I give you an example for your reference:
let data = [{
title: "string",
data: "2022-05-01",
amount: 10
},
{
title: "string",
data: "2022-04-01",
amount: 10
},
{
title: "string",
data: "2022-03-03",
amount: 10
},
{
title: "string",
data: "2022-04-01",
amount: 10
}
];
let monthly;
for (let i = 0; i < 12; i++) {
monthly=(data.filter(gg => {
let d = new Date(gg.data);
return (d.getMonth() == i)
}));
if (monthly.length>0){
console.log(monthly);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | The KNVB |
