Всем привет, подскажите пожалуйста, как оптимизировать решение?
const arr = [
{ "date": "2019-07-31", "v": "5422" },
{ "date": "2019-06-30", "v": "5220" },
{ "date": "2019-05-31", "v": "5365" },
{ "date": "2019-08-31", "v": "5451" },
{ "date": "2019-09-30", "v": "5303" },
{ "date": "2020-03-31", "v": "5654" },
{ "date": "2019-10-31", "v": "5509" },
{ "date": "2019-12-31", "v": "5567" },
{ "date": "2020-01-31", "v": "5597" },
{ "date": "2019-11-30", "v": "5359" },
{ "date": "2020-02-28", "v": "5082" },
{ "date": "2020-04-14", "v": "2567" }
];
/* result = {
"2019": [
"07-31",
"08-22"
],
"2020": [
"01-01"
"02-22"
]
} */
Поправьте, где ошибся или как луче сделать?
const arr1 = [
{ "date": "2019-07-31", "v": "5422" },
{ "date": "2019-06-30", "v": "5220" },
{ "date": "2019-05-31", "v": "5365" },
{ "date": "2019-08-31", "v": "5451" },
{ "date": "2019-09-30", "v": "5303" },
{ "date": "2020-03-31", "v": "5654" },
{ "date": "2019-10-31", "v": "5509" },
{ "date": "2019-12-31", "v": "5567" },
{ "date": "2020-01-31", "v": "5597" },
{ "date": "2019-11-30", "v": "5359" },
{ "date": "2020-02-28", "v": "5082" },
{ "date": "2020-04-14", "v": "2567" }
];
function f(arr) {
const dateList = operations.map((item) => {
return new Date(item.date);
}).sort(function(a, b) {
return a - b;
});
let res = {};
dateList.forEach(item => {
if (!res[item.getFullYear()]) {
res[item.getFullYear()] = [];
}
res[item.getFullYear()].push(`${item.getDate()}-${item.getMonth()}`)
});
return res;
}
console.log(f(arr1));