Skip to content
Advertisement

Transform JSON into required format

Here is my input JSON

  [
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Choclate"
            },
            "child": {
                "value": null,
                "filterable_value": "EMPTY"
            },
            "qty": {
                "value": 21052631.657999996,
                "rendered": "21.05M"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Choclate"
            },
            "child": {
                "value": "ABC"
            },
            "qty": {
                "value": 505567,
                "rendered": "505.57K",
                "filterable_value": "505567.0"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Choclate"
            },
            "child": {
                "value": "XYZ"
            },
            "qty": {
                "value": 33796589.61000001,
                "rendered": "33.80M"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Choclate"
            },
            "child": {
                "value": "DEF"
            },
            "qty": {
                "value": 1638886.7959999999,
                "rendered": "1.64M"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Choclate"
            },
            "child": {
                "value": "IJK"
            },
            "qty": {
                "value": 1227540.6669999997,
                "rendered": "1.23M"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Choclate"
            },
            "child": {
                "value": "MILLIIJK"
            },
            "qty": {
                "value": 624,
                "rendered": "624",
                "filterable_value": "624.0"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Choclate"
            },
            "child": {
                "value": "USP"
            },
            "qty": {
                "value": 19.326,
                "rendered": "19.3"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Drink"
            },
            "child": {
                "value": null,
                "filterable_value": "EMPTY"
            },
            "qty": {
                "value": 656562.255,
                "rendered": "656.56K"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Drink"
            },
            "child": {
                "value": "ABC"
            },
            "qty": {
                "value": 882010439.286,
                "rendered": "882.01M"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Drink"
            },
            "child": {
                "value": "XYZ"
            },
            "qty": {
                "value": 56100,
                "rendered": "56.10K",
                "filterable_value": "56100.0"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Drink"
            },
            "child": {
                "value": "DEF"
            },
            "qty": {
                "value": 63277400.964,
                "rendered": "63.28M"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Drink"
            },
            "child": {
                "value": "IJK"
            },
            "qty": {
                "value": 100239.551,
                "rendered": "100.24K"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Drink"
            },
            "child": {
                "value": "MNO"
            },
            "qty": {
                "value": 46763.176999999996,
                "rendered": "46.76K"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Drink"
            },
            "child": {
                "value": "THOUSAND PIECES"
            },
            "qty": {
                "value": 52889941.824999996,
                "rendered": "52.89M"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Drink"
            },
            "child": {
                "value": "rest"
            },
            "qty": {
                "value": 42168373.635000005,
                "rendered": "42.17M"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "FOOD"
            },
            "child": {
                "value": null,
                "filterable_value": "EMPTY"
            },
            "qty": {
                "value": 176340,
                "rendered": "176.34K",
                "filterable_value": "176340.0"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "FOOD"
            },
            "child": {
                "value": "ABC"
            },
            "qty": {
                "value": 187727583.97800002,
                "rendered": "187.73M"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "FOOD"
            },
            "child": {
                "value": "DEF"
            },
            "qty": {
                "value": 33154.707,
                "rendered": "33.15K"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "FOOD"
            },
            "child": {
                "value": "IJK"
            },
            "qty": {
                "value": 1079,
                "rendered": "1.08K",
                "filterable_value": "1079.0"
            }
        }
]

I need to transform json w.r.t parent category and what ever child json elements w.r.t parent to be in one format. All should be dynamic no hard coding comparing with strings.

Need to transform my input json to below sampleformat,

[
  {
    "Choclate": [
     {
          null: [
        {
            "2022-05-01": {
              "value": 21052631.657999996
            }
          },
          {
            "2022-04-24":{
"value": 505765             
          }
          }
        ]
      },
      {
          "ABC": [
        {
            "2022-05-01": {
              "value": 505567
            }
          }
          
        ]
      },
      {
        "XYZ": [
        
          {
            "2021-11-09": {
              "value": 33796589.61000001
            }
          },
          
          {
            "2022-09-08": {
              "value": 8000
            }
          }
          
        ]
      }
      
      ]
  },
  {
    "Drink":[
{
          "ABC": [
        {
            "2022-05-01": {
              "value": 882010439.286
            }
          }
          
        ]
      },
      {
          "XYZ": [
        {
            "2022-05-01": {
              "value":56100
            }
          }
          
        ]
      }
      
]   
  }
  ]

I tried separating parents and child values in an array and loop it and push. But not succeeded. Please suggest me how can i transform my input json to required output json format.

const data =[
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Choclate"
            },
            "child": {
                "value": null,
                "filterable_value": "EMPTY"
            },
            "qty": {
                "value": 21052631.657999996,
                "rendered": "21.05M"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Choclate"
            },
            "child": {
                "value": "ABC"
            },
            "qty": {
                "value": 505567,
                "rendered": "505.57K",
                "filterable_value": "505567.0"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Choclate"
            },
            "child": {
                "value": "XYZ"
            },
            "qty": {
                "value": 33796589.61000001,
                "rendered": "33.80M"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Choclate"
            },
            "child": {
                "value": "DEF"
            },
            "qty": {
                "value": 1638886.7959999999,
                "rendered": "1.64M"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Choclate"
            },
            "child": {
                "value": "IJK"
            },
            "qty": {
                "value": 1227540.6669999997,
                "rendered": "1.23M"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Choclate"
            },
            "child": {
                "value": "MILLIIJK"
            },
            "qty": {
                "value": 624,
                "rendered": "624",
                "filterable_value": "624.0"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Choclate"
            },
            "child": {
                "value": "USP"
            },
            "qty": {
                "value": 19.326,
                "rendered": "19.3"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Drink"
            },
            "child": {
                "value": null,
                "filterable_value": "EMPTY"
            },
            "qty": {
                "value": 656562.255,
                "rendered": "656.56K"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Drink"
            },
            "child": {
                "value": "ABC"
            },
            "qty": {
                "value": 882010439.286,
                "rendered": "882.01M"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Drink"
            },
            "child": {
                "value": "XYZ"
            },
            "qty": {
                "value": 56100,
                "rendered": "56.10K",
                "filterable_value": "56100.0"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Drink"
            },
            "child": {
                "value": "DEF"
            },
            "qty": {
                "value": 63277400.964,
                "rendered": "63.28M"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Drink"
            },
            "child": {
                "value": "IJK"
            },
            "qty": {
                "value": 100239.551,
                "rendered": "100.24K"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Drink"
            },
            "child": {
                "value": "MNO"
            },
            "qty": {
                "value": 46763.176999999996,
                "rendered": "46.76K"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Drink"
            },
            "child": {
                "value": "THOUSAND PIECES"
            },
            "qty": {
                "value": 52889941.824999996,
                "rendered": "52.89M"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Drink"
            },
            "child": {
                "value": "rest"
            },
            "qty": {
                "value": 42168373.635000005,
                "rendered": "42.17M"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "FOOD"
            },
            "child": {
                "value": null,
                "filterable_value": "EMPTY"
            },
            "qty": {
                "value": 176340,
                "rendered": "176.34K",
                "filterable_value": "176340.0"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "FOOD"
            },
            "child": {
                "value": "ABC"
            },
            "qty": {
                "value": 187727583.97800002,
                "rendered": "187.73M"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "FOOD"
            },
            "child": {
                "value": "DEF"
            },
            "qty": {
                "value": 33154.707,
                "rendered": "33.15K"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "FOOD"
            },
            "child": {
                "value": "IJK"
            },
            "qty": {
                "value": 1079,
                "rendered": "1.08K",
                "filterable_value": "1079.0"
            }
        }
];

let uniqueparent = [...new Set(data.map(item => item['parent']['value']))];
let uniquechild = [...new Set(data.map(item => item['child']['value']))];

console.log(uniqueparent);
console.log(uniquechild);

But from here, am not getting any idea how to transform my data into my desired json

Advertisement

Answer

This is prefect for me:

const data = [
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Choclate"
            },
            "child": {
                "value": null,
                "filterable_value": "EMPTY"
            },
            "qty": {
                "value": 21052631.657999996,
                "rendered": "21.05M"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Choclate"
            },
            "child": {
                "value": "ABC"
            },
            "qty": {
                "value": 505567,
                "rendered": "505.57K",
                "filterable_value": "505567.0"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Choclate"
            },
            "child": {
                "value": "XYZ"
            },
            "qty": {
                "value": 33796589.61000001,
                "rendered": "33.80M"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Choclate"
            },
            "child": {
                "value": "DEF"
            },
            "qty": {
                "value": 1638886.7959999999,
                "rendered": "1.64M"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Choclate"
            },
            "child": {
                "value": "IJK"
            },
            "qty": {
                "value": 1227540.6669999997,
                "rendered": "1.23M"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Choclate"
            },
            "child": {
                "value": "MILLIIJK"
            },
            "qty": {
                "value": 624,
                "rendered": "624",
                "filterable_value": "624.0"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Choclate"
            },
            "child": {
                "value": "USP"
            },
            "qty": {
                "value": 19.326,
                "rendered": "19.3"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Drink"
            },
            "child": {
                "value": null,
                "filterable_value": "EMPTY"
            },
            "qty": {
                "value": 656562.255,
                "rendered": "656.56K"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Drink"
            },
            "child": {
                "value": "ABC"
            },
            "qty": {
                "value": 882010439.286,
                "rendered": "882.01M"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Drink"
            },
            "child": {
                "value": "XYZ"
            },
            "qty": {
                "value": 56100,
                "rendered": "56.10K",
                "filterable_value": "56100.0"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Drink"
            },
            "child": {
                "value": "DEF"
            },
            "qty": {
                "value": 63277400.964,
                "rendered": "63.28M"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Drink"
            },
            "child": {
                "value": "IJK"
            },
            "qty": {
                "value": 100239.551,
                "rendered": "100.24K"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Drink"
            },
            "child": {
                "value": "MNO"
            },
            "qty": {
                "value": 46763.176999999996,
                "rendered": "46.76K"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Drink"
            },
            "child": {
                "value": "THOUSAND PIECES"
            },
            "qty": {
                "value": 52889941.824999996,
                "rendered": "52.89M"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Drink"
            },
            "child": {
                "value": "rest"
            },
            "qty": {
                "value": 42168373.635000005,
                "rendered": "42.17M"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "FOOD"
            },
            "child": {
                "value": null,
                "filterable_value": "EMPTY"
            },
            "qty": {
                "value": 176340,
                "rendered": "176.34K",
                "filterable_value": "176340.0"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "FOOD"
            },
            "child": {
                "value": "ABC"
            },
            "qty": {
                "value": 187727583.97800002,
                "rendered": "187.73M"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "FOOD"
            },
            "child": {
                "value": "DEF"
            },
            "qty": {
                "value": 33154.707,
                "rendered": "33.15K"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "FOOD"
            },
            "child": {
                "value": "IJK"
            },
            "qty": {
                "value": 1079,
                "rendered": "1.08K",
                "filterable_value": "1079.0"
            }
        }
];

const formattedData = data.reduce((result, el) => {
    const parentName = el.parent.value;
    const childName = el.child.value;
    const dateValue = el.date.value;
    
    if (!result[parentName]) result[parentName] = {};
    
    const parent = result[parentName];
    
    if (!parent[childName]) parent[childName] = {};
    
    const child = parent[childName];
    
    if (!child[dateValue]) child[dateValue] = el.qty.value;
    
    return result;
}, {});

console.log(formattedData);

This is what I suggest to you if you really need an object in "date" property:

const data = [
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Choclate"
            },
            "child": {
                "value": null,
                "filterable_value": "EMPTY"
            },
            "qty": {
                "value": 21052631.657999996,
                "rendered": "21.05M"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Choclate"
            },
            "child": {
                "value": "ABC"
            },
            "qty": {
                "value": 505567,
                "rendered": "505.57K",
                "filterable_value": "505567.0"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Choclate"
            },
            "child": {
                "value": "XYZ"
            },
            "qty": {
                "value": 33796589.61000001,
                "rendered": "33.80M"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Choclate"
            },
            "child": {
                "value": "DEF"
            },
            "qty": {
                "value": 1638886.7959999999,
                "rendered": "1.64M"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Choclate"
            },
            "child": {
                "value": "IJK"
            },
            "qty": {
                "value": 1227540.6669999997,
                "rendered": "1.23M"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Choclate"
            },
            "child": {
                "value": "MILLIIJK"
            },
            "qty": {
                "value": 624,
                "rendered": "624",
                "filterable_value": "624.0"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Choclate"
            },
            "child": {
                "value": "USP"
            },
            "qty": {
                "value": 19.326,
                "rendered": "19.3"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Drink"
            },
            "child": {
                "value": null,
                "filterable_value": "EMPTY"
            },
            "qty": {
                "value": 656562.255,
                "rendered": "656.56K"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Drink"
            },
            "child": {
                "value": "ABC"
            },
            "qty": {
                "value": 882010439.286,
                "rendered": "882.01M"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Drink"
            },
            "child": {
                "value": "XYZ"
            },
            "qty": {
                "value": 56100,
                "rendered": "56.10K",
                "filterable_value": "56100.0"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Drink"
            },
            "child": {
                "value": "DEF"
            },
            "qty": {
                "value": 63277400.964,
                "rendered": "63.28M"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Drink"
            },
            "child": {
                "value": "IJK"
            },
            "qty": {
                "value": 100239.551,
                "rendered": "100.24K"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Drink"
            },
            "child": {
                "value": "MNO"
            },
            "qty": {
                "value": 46763.176999999996,
                "rendered": "46.76K"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Drink"
            },
            "child": {
                "value": "THOUSAND PIECES"
            },
            "qty": {
                "value": 52889941.824999996,
                "rendered": "52.89M"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Drink"
            },
            "child": {
                "value": "rest"
            },
            "qty": {
                "value": 42168373.635000005,
                "rendered": "42.17M"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "FOOD"
            },
            "child": {
                "value": null,
                "filterable_value": "EMPTY"
            },
            "qty": {
                "value": 176340,
                "rendered": "176.34K",
                "filterable_value": "176340.0"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "FOOD"
            },
            "child": {
                "value": "ABC"
            },
            "qty": {
                "value": 187727583.97800002,
                "rendered": "187.73M"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "FOOD"
            },
            "child": {
                "value": "DEF"
            },
            "qty": {
                "value": 33154.707,
                "rendered": "33.15K"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "FOOD"
            },
            "child": {
                "value": "IJK"
            },
            "qty": {
                "value": 1079,
                "rendered": "1.08K",
                "filterable_value": "1079.0"
            }
        }
];

const formattedData = data.reduce((result, el) => {
    const parentName = el.parent.value;
    const childName = el.child.value;
    const dateValue = el.date.value;
    
    if (!result[parentName]) result[parentName] = {};
    
    const parent = result[parentName];
    
    if (!parent[childName]) parent[childName] = {};
    
    const child = parent[childName];
    
    if (!child[dateValue]) child[dateValue] = {value: el.qty.value};
    
    return result;
}, {});

console.log(formattedData);

This is what you actually want (There is also an result as in previous Code Snipept. You can get it by typing resultMap instead of resultArr at the end):

const data = [
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Choclate"
            },
            "child": {
                "value": null,
                "filterable_value": "EMPTY"
            },
            "qty": {
                "value": 21052631.657999996,
                "rendered": "21.05M"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Choclate"
            },
            "child": {
                "value": "ABC"
            },
            "qty": {
                "value": 505567,
                "rendered": "505.57K",
                "filterable_value": "505567.0"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Choclate"
            },
            "child": {
                "value": "XYZ"
            },
            "qty": {
                "value": 33796589.61000001,
                "rendered": "33.80M"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Choclate"
            },
            "child": {
                "value": "DEF"
            },
            "qty": {
                "value": 1638886.7959999999,
                "rendered": "1.64M"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Choclate"
            },
            "child": {
                "value": "IJK"
            },
            "qty": {
                "value": 1227540.6669999997,
                "rendered": "1.23M"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Choclate"
            },
            "child": {
                "value": "MILLIIJK"
            },
            "qty": {
                "value": 624,
                "rendered": "624",
                "filterable_value": "624.0"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Choclate"
            },
            "child": {
                "value": "USP"
            },
            "qty": {
                "value": 19.326,
                "rendered": "19.3"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Drink"
            },
            "child": {
                "value": null,
                "filterable_value": "EMPTY"
            },
            "qty": {
                "value": 656562.255,
                "rendered": "656.56K"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Drink"
            },
            "child": {
                "value": "ABC"
            },
            "qty": {
                "value": 882010439.286,
                "rendered": "882.01M"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Drink"
            },
            "child": {
                "value": "XYZ"
            },
            "qty": {
                "value": 56100,
                "rendered": "56.10K",
                "filterable_value": "56100.0"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Drink"
            },
            "child": {
                "value": "DEF"
            },
            "qty": {
                "value": 63277400.964,
                "rendered": "63.28M"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Drink"
            },
            "child": {
                "value": "IJK"
            },
            "qty": {
                "value": 100239.551,
                "rendered": "100.24K"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Drink"
            },
            "child": {
                "value": "MNO"
            },
            "qty": {
                "value": 46763.176999999996,
                "rendered": "46.76K"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Drink"
            },
            "child": {
                "value": "THOUSAND PIECES"
            },
            "qty": {
                "value": 52889941.824999996,
                "rendered": "52.89M"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "Drink"
            },
            "child": {
                "value": "rest"
            },
            "qty": {
                "value": 42168373.635000005,
                "rendered": "42.17M"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "FOOD"
            },
            "child": {
                "value": null,
                "filterable_value": "EMPTY"
            },
            "qty": {
                "value": 176340,
                "rendered": "176.34K",
                "filterable_value": "176340.0"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "FOOD"
            },
            "child": {
                "value": "ABC"
            },
            "qty": {
                "value": 187727583.97800002,
                "rendered": "187.73M"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "FOOD"
            },
            "child": {
                "value": "DEF"
            },
            "qty": {
                "value": 33154.707,
                "rendered": "33.15K"
            }
        },
        {
            "date": {
                "value": "2022-05-01"
            },
            "parent": {
                "value": "FOOD"
            },
            "child": {
                "value": "IJK"
            },
            "qty": {
                "value": 1079,
                "rendered": "1.08K",
                "filterable_value": "1079.0"
            }
        }
];

const formattedData = data.reduce((resultTypes, el) => {
    const resultMap = resultTypes.resultMap;
    const resultArr = resultTypes.resultArr;
    
    const parentName = el.parent.value;
    const childName = el.child.value;
    const dateValue = el.date.value;
    
    if (!resultMap[parentName]) {
        resultMap[parentName] = {};
        resultArr.push({[parentName]: []})
    }
    
    const parentInMap = resultMap[parentName];
    const parentInArr = resultArr.find(parents => parents[parentName])[parentName];
    
    if (!parentInMap[childName]) {
        parentInMap[childName] = {};
        parentInArr.push({[childName]: []})
    }
    
    const childInMap = parentInMap[childName];
    const childInArr = parentInArr.find(children => children[childName])[childName];
    
    if (!childInMap[dateValue]) {
        childInMap[dateValue] = {value: el.qty.value};
        childInArr.push({[dateValue]: {value: el.qty.value}});
    }
    
    return resultTypes;
}, {resultMap: {}, resultArr: []}).resultArr;

console.log(formattedData);
Advertisement