How to push rows of json array as a column in react

Issue

This Content is from Stack Overflow. Question asked by lock

i am trying to push my rows as a column in react json array.

I have distinct coaGrpId, and sitecode

coaGrpId=  [355,37,93,85,97,135, 83,354,353,356,65,136,357]
categories=[ "ANKL","AYDH", "BHLR","BIAL","BLSH" (40 more such values)]

Having a json data fetching from which is having more than 30k records.
Now i have to push my rows(opening,PrdDr,Prdcr,closing) as column categories wise.

below is the sample of coaGrpid wise json data

sourceRows =
    {
      135:{
        "BIAL": {
            "siteCode": "BIAL",
            "ldgrGrp": "G00",
            "coaGrpNm": "Sales Accounts",
            "coaGrpCode": "SA",
            "coaGrpId": 135,
            "opening": 0,
            "prdDr": 45,
            "prdCr": 0,
            "closing": 45
        },
        "DRWT": {
            "siteCode": "DRWT",
            "ldgrGrp": "G00",
            "coaGrpNm": "Current Liabilities",
            "coaGrpCode": "CL",
            "coaGrpId": 135,
            "opening": 0,
            "prdDr": 0,
            "prdCr": 119,
            "closing": -119
        },
        "NMIA": {
            "siteCode": "NMIA",
            "ldgrGrp": "G00",
            "coaGrpNm": "Sales Accounts",
            "coaGrpCode": "SA",
            "coaGrpId": 135,
            "opening": 0,
            "prdDr": 0,
            "prdCr": 20602,
            "closing": -20602
        },
        "SGSH": {
            "siteCode": "SGSH",
            "ldgrGrp": "G00",
            "coaGrpNm": "Current Liabilities",
            "coaGrpCode": "CL",
            "coaGrpId": 135,
            "opening": 0,
            "prdDr": 0,
            "prdCr": 1400,
            "closing": -1400
        },
        "VLSD": {
            "siteCode": "VLSD",
            "ldgrGrp": "G00",
            "coaGrpNm": "Current Assets",
            "coaGrpCode": "CA",
            "coaGrpId": 135,
            "opening": 0,
            "prdDr": 0,
            "prdCr": 2370,
            "closing": -2370
        }
      }
    }

and i want output in below format

{
{
    "ldgrGrp": "G00",
        "coaGrpNm": "Sales Accounts",
        "coaGrpCode": "SA",
        "coaGrpId": 135,
    "bial_opening": 0,
        "bial_prdDr": 0,
        "bial_prdCr": 119,
        "bial_closing": -119,
    "nmia_opening": 0,
        "nmia_prdDr": 0,
        "nmia_prdCr": 20602,
        "nmia_closing": -20602

},
{
    "ldgrGrp": "G00",
        "coaGrpNm": "Current Liabilities",
        "coaGrpCode": "CL",
        "coaGrpId": 135,
    "opening": 0,
        "drwt_prdDr": 0,
        "drwt_prdCr": 119,
        "drwt_closing": -119
    "sgsh_opening": 0,
        "sgsh_prdDr": 0,
        "sgsh_prdCr": 1400,
        "sgsh_closing": -1400

},
{
    "ldgrGrp": "G00",
        "coaGrpNm": "Current Assets",
        "coaGrpCode": "CA",
    "coaGrpId": 135,
    "vlsd_opening": 0,
        "vlsd_prdDr": 0,
        "vlsd_prdCr": 2370,
        "vlsd_closing": -2370
}
}

function ShowPivotedData() {

    const coaGrpId = Array(...new Set(ldgrwisesum.map((x) => x.coaGrpId)));
    const categories = Array(...new Set(ldgrwisesum.map((x) => x.siteCode)));
    const sourceRows = {};
    ldgrwisesum.forEach((row) => {
        if (!sourceRows[row.coaGrpId]) {
            sourceRows[row.coaGrpId] = {};
        }
        sourceRows[row.coaGrpId][row.siteCode] = row;
    });
    const destination = [];
    coaGrpId.forEach((id) => {
        categories.forEach((site) => {
            const sourceRow = sourceRows[id][categories[0]];
            const destRow = {
                coaGrpId: id,
                siteCode: site,
                opening: //Opening as per site and coaGrpId wise
                prdDr: //Opening as per site and coaGrpId wise
                prdCr: //Opening as per site and coaGrpId wise
                closing: //Opening as per site and coaGrpId wise
            };
           
            destination.push(destRow);
        });
    });

    console.log(destination);
}



Solution

This question is not yet answered, be the first one who answer using the comment. Later the confirmed answer will be published as the solution.

This Question and Answer are collected from stackoverflow and tested by JTuto community, is licensed under the terms of CC BY-SA 2.5. - CC BY-SA 3.0. - CC BY-SA 4.0.

people found this article helpful. What about you?