Group Object have same Property in Array Object in Javascript

53 Views Asked by At

I have array object, so i need group object have same BrandId, with special property.

ArayExample:

[
{
            "BrandId": "5a52f163b56fbc1164c3f69a",
            "ActionCode": "MapNewInsert",
            "Total": 126,
            "TotalProduct": 127,
            "TotalBrand": 2
        },
        {
            "BrandId": "5a52f163b56fbc1164c3f69a",
            "ActionCode": "MapNewUpdate",
            "Total": 0,
            "TotalProduct": 127,
            "TotalBrand": 2
        },
        {
            "BrandId": "5a52f163b56fbc1164c3f69a",
            "ActionCode": "VerifyUpdate",
            "Total": 0,
            "TotalProduct": 127,
            "TotalBrand": 2
        },
        {
            "BrandId": "5a52f163b56fbc1164c3f69a",
            "ActionCode": "VerifyDelete",
            "Total": 0,
            "TotalProduct": 127,
            "TotalBrand": 2
        },
        {
            "BrandId": "5a52f163b56fbc1164c3f69a",
            "ActionCode": "Solved",
            "Total": 1,
            "TotalProduct": 127,
            "TotalBrand": 2
        },
        {
            "BrandId": "5c10d62821cdfb2448a10a9c",
            "ActionCode": "MapNewInsert",
            "Total": 398,
            "TotalProduct": 398,
            "TotalBrand": 2
        }
]

Special is: value in "ActionCode" become a key and Total in that item become value of that key.

Example: ActionCode have value is MapNewInsert and in this item have Total number value. That become: "MapNewInsert":"398(Total value)" Here is result i want:

    [
       
            "BrandId": "5a52f163b56fbc1164c3f69a",
            "Statistics": {
                "MapNewDelete": 0
                "MapNewInsert": 126
                "MapNewUpdate": 0
                "Solved": 1
            }
        },
        {
            "BrandId": "5c10d62821cdfb2448a10a9c",
            "Statistics": {
                "MapNewDelete": 0
                "MapNewInsert": 398
                "MapNewUpdate": 0
                "Solved": 0
            }
        }
    ]

Thanks so much for help

1

There are 1 best solutions below

3
On BEST ANSWER

It can be done with Array Reduce Function . The Result is not exactly the same but you can see if it can work for you .

let arr = [
            {
                BrandId: '5a52f163b56fbc1164c3f69a',
                ActionCode: 'MapNewInsert',
                Total: 126,
                TotalProduct: 127,
                TotalBrand: 2
            },
            {
                BrandId: '5a52f163b56fbc1164c3f69a',
                ActionCode: 'MapNewUpdate',
                Total: 0,
                TotalProduct: 127,
                TotalBrand: 2
            },
            {
                BrandId: '5a52f163b56fbc1164c3f69a',
                ActionCode: 'VerifyUpdate',
                Total: 0,
                TotalProduct: 127,
                TotalBrand: 2
            },
            {
                BrandId: '5a52f163b56fbc1164c3f69a',
                ActionCode: 'VerifyDelete',
                Total: 0,
                TotalProduct: 127,
                TotalBrand: 2
            },
            {
                BrandId: '5a52f163b56fbc1164c3f69a',
                ActionCode: 'Solved',
                Total: 1,
                TotalProduct: 127,
                TotalBrand: 2
            },
            {
                BrandId: '5c10d62821cdfb2448a10a9c',
                ActionCode: 'MapNewInsert',
                Total: 398,
                TotalProduct: 398,
                TotalBrand: 2
            }
        ];
        const result = [
            ...arr
                .reduce((r, o) => {
                    const record = r.get(o.BrandId) || {};
                    r.set(o.BrandId, {
                        BrandId: o.BrandId,
                        Statistics: {
                            [o.ActionCode]: o.Total,
                            ...record.Statistics
                        }
                    });
                    return r;
                }, new Map())
                .values()
        ];

        console.log(result);