Currently, I am working on creating a new array of objects that are sorted by year. Can someone suggest the most efficient method to iterate through an array of objects structured like this:
[
{id: 42, count: 250, month: "January", year: 2024}
{id: 37, count: 2023, month: "January", year: 2023}
{id: 1, count: 1111, month: "January", year: 2022}
{id: 2, count: 100, month: "February", year: 2022}
{id: 3, count: 500, month: "March", year: 2022}
{id: 6, count: 1, month: "April", year: 2022}
{id: 8, count: 500, month: "May", year: 2022}
];
I want to transform it into a new array structured like this:
[
{
2024: [{id: 42, count: 250, month: "January", year: 2024}]
},
{
2023: [{id: 37, count: 2023, month: "January", year: 2023}]
},
{
2022: [
{id: 1, count: 1111, month: "January", year: 2022},
{id: 2, count: 100, month: "February", year: 2022},
{id: 3, count: 500, month: "March", year: 2022},
{id: 6, count: 1, month: "April", year: 2022},
{id: 8, count: 500, month: "May", year: 2022}
]
},
]