如何将嵌套对象转换为 javascript 中的对象数组?

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

我收到来自服务器的 JSON 响应,“IN01”、“IN02”和“2021”、“2022”这些是动态对象键。我想将此结构转换为其他格式。 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"

我想将上面的JSON数据转换成下面提到的格式

{
    "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"
                }
              ]
        }
    ]
}



非常感谢您的帮助。谢谢!

使用Object.keys获取对象的所有键。使用示例

var json = document.getElementById("json").value;
var jsonObject = JSON.parse(json);

jsonObject.holidayCalendar.forEach(function(x) {
  Object.keys(x).forEach(function(key) {
    console.log(key, x[key]);
  });
})
<textarea id="json">
{
  "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"
            }
          ]
        }
      ]
    }
  ]
}
</textarea>

您可以使用 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; }

如果你想使用 days 而不是 value 那么你可以这样做:

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; }