Home > Software design >  Typescrit reduce to break array of objects into separate arrays based on a property
Typescrit reduce to break array of objects into separate arrays based on a property

Time:07-23

I have an array like this:

    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 type of this array is: StatisticsOfScrapDeliveriesItems[]

What I want is to split up the array into arrays that have objects which have same materialID 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",
        }
    ]
]

So I can acces easily to the objects with specific materialId.

I have found solutions with javascript reduce, but I am using typescript and those solutions give me types errors...

CodePudding user response:

const 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',
  },
];

const data = statisticsOfScrapDeliveriesItems.reduce<
  Record<string, typeof statisticsOfScrapDeliveriesItems>
>((p, c) => {
  p[c.materialId] = p[c.materialId] || [];
  p[c.materialId].push(c);
  return p;
}, {});

console.log(data);

  • Related