How to convert nested object to array of object in javascript?

Sooraj Jose

I have a JSON response from the server, "IN01", "IN02" and "2021", "2022" these are dynamic object keys. I want to covert this structure to some other format. How to do with javascript?

{
  "holidayCalendar": [
    {
      "IN01": [
        {
          "2021": [
            {
              "month": "1",
              "value": "0101111110111011011111101011110"
            },
            {
              "month": "2",
              "value": "1111110111111011111101111110"
            },
            {
              "month": "3",
              "value": "1111110111111011111101111110011"
            }
          ]
        },
        {
          "2022": [
            {
              "month": "4",
              "value": "0011111101111110111111011011101"
            },
            {
              "month": "5",
              "value": "1111101111110111111011111101"
            },
            {
              "month": "6",
              "value": "1111101111110111111011111101111"
            }
          ]
        }
      ]
    },
    {
      "IN02": [
        {
          "2021": [
            {
              "month": "1",
              "value": "0101111110111011011111101011110"
            },
            {
              "month": "2",
              "value": "1111110111111011111101111110"
            },
            {
              "month": "3",
              "value": "1111110111111011111101111110011"
            }
          ]
        },
        {
          "2022": [
            {
              "month": "4",
              "value": "0011111101111110111111011011101"
            },
            {
              "month": "5",
              "value": "1111101111110111111011111101"
            }
          ]
        }
      ]
    }
  ]
}

Here key can be any value instead of "IN01", "IN02" etc also "2021" , "2021"

I want to convert the above JSON data to below-mentioned format

{
    "holidayCalendar" : [
        {
            "location" : "IN01",
            "year" : "2021",
            "holidays" : [
                {
                    "month": "1",
                    "value": "0101111110111011011111101011110"
                  },
                  {
                    "month": "2",
                    "value": "1111110111111011111101111110"
                  },
                  {
                    "month": "3",
                    "value": "1111110111111011111101111110011"
                  }
            ]

        },
        {
            "location" : "IN01",
            "year" : "2022",
            "holidays" : [{
                "month": "4",
                "value": "0011111101111110111111011011101"
              },
              {
                "month": "5",
                "value": "1111101111110111111011111101"
              },
              {
                "month": "6",
                "value": "1111101111110111111011111101111"
              }
            ]
        },
        {
            "location" : "IN02",
            "year" : "2021",
            "holidays" : [
                {
                  "month": "1",
                  "value": "0101111110111011011111101011110"
                },
                {
                  "month": "2",
                  "value": "1111110111111011111101111110"
                },
                {
                  "month": "3",
                  "value": "1111110111111011111101111110011"
                }
              ]

        },
        {
            "location" : "IN02",
            "year" : "2022",
            "holidays" : [
                {
                  "month": "4",
                  "value": "0011111101111110111111011011101"
                },
                {
                  "month": "5",
                  "value": "1111101111110111111011111101"
                }
              ]
        }
    ]
}



Really appreciated your help. Thank You !

decpk

You can easily achieve the result using flatMap, Object.entries

const obj = {
  holidayCalendar: [
    {
      IN01: [
        {
          "2021": [
            {
              month: "1",
              value: "0101111110111011011111101011110",
            },
            {
              month: "2",
              value: "1111110111111011111101111110",
            },
            {
              month: "3",
              value: "1111110111111011111101111110011",
            },
          ],
        },
        {
          "2022": [
            {
              month: "4",
              value: "0011111101111110111111011011101",
            },
            {
              month: "5",
              value: "1111101111110111111011111101",
            },
            {
              month: "6",
              value: "1111101111110111111011111101111",
            },
          ],
        },
      ],
    },
    {
      IN02: [
        {
          "2021": [
            {
              month: "1",
              value: "0101111110111011011111101011110",
            },
            {
              month: "2",
              value: "1111110111111011111101111110",
            },
            {
              month: "3",
              value: "1111110111111011111101111110011",
            },
          ],
        },
        {
          "2022": [
            {
              month: "4",
              value: "0011111101111110111111011011101",
            },
            {
              month: "5",
              value: "1111101111110111111011111101",
            },
          ],
        },
      ],
    },
  ],
};

const result = {
  ...obj,
  holidayCalendar: obj.holidayCalendar.flatMap((obj) =>
    Object.entries(obj).flatMap(([location, v]) =>
      v.flatMap((o) =>
        Object.entries(o).map(([year, holidays]) => ({
          location,
          year,
          holidays,
        }))
      )
    )
  ),
};
console.log(result);
/* This is not a part of answer. It is just to give the output full height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }

If you want to use days instead of value then you can do as:

const obj = {
  holidayCalendar: [
    {
      IN01: [
        {
          "2021": [
            {
              month: "1",
              value: "0101111110111011011111101011110",
            },
            {
              month: "2",
              value: "1111110111111011111101111110",
            },
            {
              month: "3",
              value: "1111110111111011111101111110011",
            },
          ],
        },
        {
          "2022": [
            {
              month: "4",
              value: "0011111101111110111111011011101",
            },
            {
              month: "5",
              value: "1111101111110111111011111101",
            },
            {
              month: "6",
              value: "1111101111110111111011111101111",
            },
          ],
        },
      ],
    },
    {
      IN02: [
        {
          "2021": [
            {
              month: "1",
              value: "0101111110111011011111101011110",
            },
            {
              month: "2",
              value: "1111110111111011111101111110",
            },
            {
              month: "3",
              value: "1111110111111011111101111110011",
            },
          ],
        },
        {
          "2022": [
            {
              month: "4",
              value: "0011111101111110111111011011101",
            },
            {
              month: "5",
              value: "1111101111110111111011111101",
            },
          ],
        },
      ],
    },
  ],
};

const result = {
  ...obj,
  holidayCalendar: obj.holidayCalendar.flatMap((obj) =>
    Object.entries(obj).flatMap(([location, v]) =>
      v.flatMap((o) =>
        Object.entries(o).map(([year, holidays]) => ({
          location,
          year,
          holidays: holidays.map(({ value, ...rest }) => ({
            ...rest,
            days: value,
          })),
        }))
      )
    )
  ),
};
console.log(result);
/* This is not a part of answer. It is just to give the output full height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to change array object to nested object in javascript

Convert nested array into object

Convert nested array to an object

How to convert an array into a nested plain object

Javascript - Convert Nested Object to Array of items

Convert Array of Objects to Nested Object in Javascript

How to convert array to object in Javascript?

How to Convert nested json to array of object in Javascript?

javascript how to convert array into object

how to convert plain array into nested object which is nested into parent object

How to convert javascript array to object

Convert nested array to nested object

How to Convert Object to Array in Javascript

how to convert class object with nested array to datatable?

How to Convert a nested array to an object

how to convert objects in array which is already in a nested object in javaScript

How to convert a JavaScript array to an object?

Convert nested object to array of object

How to convert an array of nested objects to an object with key, value pairs in JavaScript

How to convert nested Array into simple typescript object

How to convert multiple object indexes into the nested array?

is there any way to convert nested object to array in javascript

how convert my nested object to array in javascript

How to convert string array into custom nested object

How do you convert an array to a nested object in javascript?

How to convert array in Object in JavaScript?

How to convert array with nested object to object?

Convert array to nested object structure using Javascript

React - How to convert a nested object into a array