Hola buenos días espero me puedan ayudar, actualmente tengo un problema con una estructura json la cual viene de la siguiente forma,dicha estructura contiene la fecha de creación, esta tiene el siguiente formato: YYYYMMDD
[
{
"society": "1000",
"centro": "2005",
"nameStore": "TIENDA",
"dateOfCreation": "2021-01-11",
"yearCreation": "2021"
},
{
"society": "1000",
"centro": "3008",
"nameStore": "TIENDA",
"dateOfCreation": "2021-01-08",
"yearCreation": "2021"
},
{
"society": "1000",
"centro": "3006",
"nameStore": "TIENDA",
"dateOfCreation": "2021-01-09",
"yearCreation": "2021"
},
{
"society": "1000",
"centro": "3001",
"nameStore": "TIENDA",
"dateOfCreation": "2021-01-09",
"yearCreation": "2021"
},
]
y deseo obtener la siguiente estructura mas sin embargo hasta el momento no he podido lograrlo con exito:
[
{
"year": "2021",
"months": [
{
"month": "01",
"data": [
{
"society": "1000",
"centro": "3006",
"nameStore": "TIENDA",
"dateOfCreation": "2021-01-09",
"yearCreation": "2021"
},
{
"society": "1000",
"centro": "3006",
"nameStore": "TIENDA",
"dateOfCreation": "2021-01-09",
"yearCreation": "2021"
}
]
},
{
"month": "02",
"data": [
{
"society": "1000",
"centro": "3006",
"nameStore": "TIENDA",
"dateOfCreation": "2021-01-09",
"yearCreation": "2021"
},
{
"society": "1000",
"centro": "3006",
"nameStore": "TIENDA",
"dateOfCreation": "2021-01-09",
"yearCreation": "2021"
}
]
},
{
"month": "03",
"data": [
{
"society": "1000",
"centro": "3006",
"nameStore": "TIENDA",
"dateOfCreation": "2021-01-09",
"yearCreation": "2021"
},
{
"society": "1000",
"centro": "3006",
"nameStore": "TIENDA",
"dateOfCreation": "2021-01-09",
"yearCreation": "2021"
}
]
},
{
"month": "04",
"data": [
{
"society": "1000",
"centro": "3006",
"nameStore": "TIENDA",
"dateOfCreation": "2021-01-09",
"yearCreation": "2021"
},
{
"society": "1000",
"centro": "3006",
"nameStore": "TIENDA",
"dateOfCreation": "2021-01-09",
"yearCreation": "2021"
}
]
},
{
"month": "05",
"data": [
{
"society": "1000",
"centro": "3006",
"nameStore": "TIENDA",
"dateOfCreation": "2021-01-09",
"yearCreation": "2021"
},
{
"society": "1000",
"centro": "3006",
"nameStore": "TIENDA",
"dateOfCreation": "2021-01-09",
"yearCreation": "2021"
}
]
},
{
"month": "06",
"data": [
{
"society": "1000",
"centro": "3006",
"nameStore": "TIENDA",
"dateOfCreation": "2021-01-09",
"yearCreation": "2021"
},
{
"society": "1000",
"centro": "3006",
"nameStore": "TIENDA",
"dateOfCreation": "2021-01-09",
"yearCreation": "2021"
}
]
},
{
"month": "07",
"data": [
{
"society": "1000",
"centro": "3006",
"nameStore": "TIENDA",
"dateOfCreation": "2021-01-09",
"yearCreation": "2021"
},
{
"society": "1000",
"centro": "3006",
"nameStore": "TIENDA",
"dateOfCreation": "2021-01-09",
"yearCreation": "2021"
}
]
},
{
"month": "08",
"data": [
{
"society": "1000",
"centro": "3006",
"nameStore": "TIENDA",
"dateOfCreation": "2021-01-09",
"yearCreation": "2021"
},
{
"society": "1000",
"centro": "3006",
"nameStore": "TIENDA",
"dateOfCreation": "2021-01-09",
"yearCreation": "2021"
}
]
},
{
"month": "09",
"data": [
{
"society": "1000",
"centro": "3006",
"nameStore": "TIENDA",
"dateOfCreation": "2021-01-09",
"yearCreation": "2021"
},
{
"society": "1000",
"centro": "3006",
"nameStore": "TIENDA",
"dateOfCreation": "2021-01-09",
"yearCreation": "2021"
}
]
},
{
"month": "10",
"data": [
{
"society": "1000",
"centro": "3006",
"nameStore": "TIENDA",
"dateOfCreation": "2021-01-09",
"yearCreation": "2021"
},
{
"society": "1000",
"centro": "3006",
"nameStore": "TIENDA",
"dateOfCreation": "2021-01-09",
"yearCreation": "2021"
}
]
},
{
"month": "11",
"data": [
{
"society": "1000",
"centro": "3006",
"nameStore": "TIENDA",
"dateOfCreation": "2021-01-09",
"yearCreation": "2021"
},
{
"society": "1000",
"centro": "3006",
"nameStore": "TIENDA",
"dateOfCreation": "2021-01-09",
"yearCreation": "2021"
}
]
},
{
"month": "12",
"data": [
{
"society": "1000",
"centro": "3006",
"nameStore": "TIENDA",
"dateOfCreation": "2021-01-09",
"yearCreation": "2021"
},
{
"society": "1000",
"centro": "3006",
"nameStore": "TIENDA",
"dateOfCreation": "2021-01-09",
"yearCreation": "2021"
}
]
}
]
},
{
"year": "2022",
"months": [
{
"month": "01",
"data": [
{
"society": "1000",
"centro": "3006",
"nameStore": "TIENDA",
"dateOfCreation": "2021-01-09",
"yearCreation": "2021"
},
{
"society": "1000",
"centro": "3006",
"nameStore": "TIENDA",
"dateOfCreation": "2021-01-09",
"yearCreation": "2021"
}
]
},
{
"month": "02",
"data": [
{
"society": "1000",
"centro": "3006",
"nameStore": "TIENDA",
"dateOfCreation": "2021-01-09",
"yearCreation": "2021"
},
{
"society": "1000",
"centro": "3006",
"nameStore": "TIENDA",
"dateOfCreation": "2021-01-09",
"yearCreation": "2021"
}
]
},
{
"month": "03",
"data": [
{
"society": "1000",
"centro": "3006",
"nameStore": "TIENDA",
"dateOfCreation": "2021-01-09",
"yearCreation": "2021"
},
{
"society": "1000",
"centro": "3006",
"nameStore": "TIENDA",
"dateOfCreation": "2021-01-09",
"yearCreation": "2021"
}
]
},
{
"month": "04",
"data": [
{
"society": "1000",
"centro": "3006",
"nameStore": "TIENDA",
"dateOfCreation": "2021-01-09",
"yearCreation": "2021"
},
{
"society": "1000",
"centro": "3006",
"nameStore": "TIENDA",
"dateOfCreation": "2021-01-09",
"yearCreation": "2021"
}
]
},
{
"month": "05",
"data": [
{
"society": "1000",
"centro": "3006",
"nameStore": "TIENDA",
"dateOfCreation": "2021-01-09",
"yearCreation": "2021"
},
{
"society": "1000",
"centro": "3006",
"nameStore": "TIENDA",
"dateOfCreation": "2021-01-09",
"yearCreation": "2021"
}
]
},
{
"month": "06",
"data": [
{
"society": "1000",
"centro": "3006",
"nameStore": "TIENDA",
"dateOfCreation": "2021-01-09",
"yearCreation": "2021"
},
{
"society": "1000",
"centro": "3006",
"nameStore": "TIENDA",
"dateOfCreation": "2021-01-09",
"yearCreation": "2021"
}
]
},
{
"month": "07",
"data": [
{
"society": "1000",
"centro": "3006",
"nameStore": "TIENDA",
"dateOfCreation": "2021-01-09",
"yearCreation": "2021"
},
{
"society": "1000",
"centro": "3006",
"nameStore": "TIENDA",
"dateOfCreation": "2021-01-09",
"yearCreation": "2021"
}
]
},
{
"month": "08",
"data": [
{
"society": "1000",
"centro": "3006",
"nameStore": "TIENDA",
"dateOfCreation": "2021-01-09",
"yearCreation": "2021"
},
{
"society": "1000",
"centro": "3006",
"nameStore": "TIENDA",
"dateOfCreation": "2021-01-09",
"yearCreation": "2021"
}
]
},
{
"month": "09",
"data": [
{
"society": "1000",
"centro": "3006",
"nameStore": "TIENDA",
"dateOfCreation": "2021-01-09",
"yearCreation": "2021"
},
{
"society": "1000",
"centro": "3006",
"nameStore": "TIENDA",
"dateOfCreation": "2021-01-09",
"yearCreation": "2021"
}
]
},
{
"month": "10",
"data": [
{
"society": "1000",
"centro": "3006",
"nameStore": "TIENDA",
"dateOfCreation": "2021-01-09",
"yearCreation": "2021"
},
{
"society": "1000",
"centro": "3006",
"nameStore": "TIENDA",
"dateOfCreation": "2021-01-09",
"yearCreation": "2021"
}
]
},
{
"month": "11",
"data": [
{
"society": "1000",
"centro": "3006",
"nameStore": "TIENDA",
"dateOfCreation": "2021-01-09",
"yearCreation": "2021"
},
{
"society": "1000",
"centro": "3006",
"nameStore": "TIENDA",
"dateOfCreation": "2021-01-09",
"yearCreation": "2021"
}
]
},
{
"month": "12",
"data": [
{
"society": "1000",
"centro": "3006",
"nameStore": "TIENDA",
"dateOfCreation": "2021-01-09",
"yearCreation": "2021"
},
{
"society": "1000",
"centro": "3006",
"nameStore": "TIENDA",
"dateOfCreation": "2021-01-09",
"yearCreation": "2021"
}
]
}
]
},
]
Hasta el momento lo mas cerca que he estado de resolver el problema ha sido mediante esta función:
groupByMonthWeek(data: any) {
var year;
return data.reduce(function (acc: any, obj: any) {
var b = obj.dateOfCreation.split(/\D/);
// Add year if not already present
if (!acc[b[0]]) acc[b[0]] = {};
year = acc[b[0]];
// Add month if not already present
if (!year[b[1]]) year[b[1]] = [];
year[b[1]].push(obj);
return acc;
}, Object.create(null));
}
Espero que alguién haya pasado por lo mismo y pueda compartirme una solución a este problema.
Gracias de antemano.