skip to Main Content

I have an array like this:

a1=[
    {
        "Domain": "xyz",
        "Classification": "prod",
        "AnnualData": [
            {
                "Year": 2029,
                "Value": 67007.41
            },
            {
                "Year": 2030,
                "Value": 163234.6
            },
            {
                "Year": 2031,
                "Value": 166388.91
            }
        ]
    },
    {
        "Domain": "xyz",
        "Classification": "prod",
        "AnnualData": [
            {
                "Year": 2029,
                "Value": 67007.41
            },
            {
                "Year": 2030,
                "Value": 163234.6
            },
            {
                "Year": 2031,
                "Value": 166388.91
            }
        ]
    }
];

I want to replace AnnualData , so that o/p look like

[
    {
        "2029": 67007.41,
        "2030": 163234.6,
        "2031": 166388.91,
        "Domain": "xyz",
        "Classification": "prod"
    },
    {
        "2029": 67007.41,
        "2030": 163234.6,
        "2031": 166388.91,
        "Domain": "xyz",
        "Classification": "prod"
    }
]

how would we do this?

I hava converted the annualdata into hashmap using list.reduce((obj, item) => ({…obj, [item.Year]: item.Value}), {});and tried to push it into main array but dint get the expected results

3

Answers


  1. const jsonString = 'your-json';
    
    const jsonArray = JSON.parse(jsonString);
    
    const resultList = [];
    
    jsonArray.forEach(item => {
        const resultItem = {
            Domain: item.Domain,
            Classification: item.Classification
        };
    
        item.AnnualData.forEach(data => {
            resultItem[data.Year] = data.Value;
        });
    
        resultList.push(resultItem);
    });
    
    const resultJsonString = JSON.stringify(resultList, null, 4);
    console.log(resultJsonString);
    
    Login or Signup to reply.
  2. You don’t use push() to modify an object. You can use Object.assign() to merge objects in place, or reassign using spread notation.

    a1.forEach(obj => {
      let annualData = Object.fromEntries(obj.AnnualData.map(({Year, Value}) => [Year, Value]));
      delete obj.AnnualData;
      Object.assign(obj, annualData);
    });
    
    console.log(a1);
    <script>
    let a1=[
        {
            "Domain": "xyz",
            "Classification": "prod",
            "AnnualData": [
                {
                    "Year": 2029,
                    "Value": 67007.41
                },
                {
                    "Year": 2030,
                    "Value": 163234.6
                },
                {
                    "Year": 2031,
                    "Value": 166388.91
                }
            ]
        },
        {
            "Domain": "xyz",
            "Classification": "prod",
            "AnnualData": [
                {
                    "Year": 2029,
                    "Value": 67007.41
                },
                {
                    "Year": 2030,
                    "Value": 163234.6
                },
                {
                    "Year": 2031,
                    "Value": 166388.91
                }
            ]
        }
    ];
    </script>
    Login or Signup to reply.
  3. I would have done it using forEach method, like this :

    let result = a1.map(item => {
        // new object
        let newData = {};
        // loop over your data
        item.AnnualData.forEach(data => {
            // set each year as a key in newData, with the number as value
            newData[data.Year] = data.Value;
        });
        // fill the other properties
        newData.Domain = item.Domain;
        newData.Classification = item.Classification;
        return newData;
    });
    

    The principe is to create a new objet, and iterate over your data to fill the new object resulting as what you want.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search