I have the following array:
statisticsOfScrapDeliveriesItems:[
{
supplierId: "0001055404",
deliveredFrom: "METALLCO AS",
centerId: "C45",
materialId: "TS0180",
},
{
supplierId: "0001055404",
deliveredFrom: "METALLCO AS",
centerId: "C45",
materialId: "TS0085",
},
{
supplierId: "0001055404",
deliveredFrom: "METALLCO AS",
centerId: "C45",
materialId: "TS0085",
},
{
supplierId: "0001055404",
deliveredFrom: "METALLCO AS",
centerId: "C45",
materialId: "TS0180",
},
{
supplierId: "0001055404",
deliveredFrom: "METALLCO AS",
centerId: "C45",
materialId: "TS0065",
},
{
supplierId: "0001055404",
deliveredFrom: "METALLCO AS",
centerId: "C45",
materialId: "TS0065",
}
]
The data type of this array is:
StatisticsOfScrapDeliveriesItems[]
What I am looking to do is separate the array into smaller arrays based on objects that share the same materialID, like so:
statisticsOfScrapDeliveriesItems:[
TS0180: [
{
supplierId: "0001055404",
deliveredFrom: "METALLCO AS",
centerId: "C45",
materialId: "TS0180",
},
{
supplierId: "0001055404",
deliveredFrom: "METALLCO AS",
centerId: "C45",
materialId: "TS0180",
}
],
TS0085: [
{
supplierId: "0001055404",
deliveredFrom: "METALLCO AS",
centerId: "C45",
materialId: "TS0085",
},
{
supplierId: "0001055404",
deliveredFrom: "METALLCO AS",
centerId: "C45",
materialId: "TS0085",
},
],
TS0065: [
{
supplierId: "0001055404",
deliveredFrom: "METALLCO AS",
centerId: "C45",
materialId: "TS0065",
},
{
supplierId: "0001055404",
deliveredFrom: "METALLCO AS",
centerId: "C45",
materialId: "TS0065",
}
]
]
This will allow me to easily access objects with a specific materialID.
I have come across solutions using JavaScript reduce, but since I am working in TypeScript, they are causing type errors...