如何在javascript中将嵌套对象转换为对象数组? [关闭]

Posted

技术标签:

【中文标题】如何在javascript中将嵌套对象转换为对象数组? [关闭]【英文标题】:How to convert nested object to array of object in javascript? [closed] 【发布时间】:2021-12-13 07:58:44 【问题描述】:

我有来自服务器的 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"
            
          ]
        
      ]
    
  ]


这里的键可以是任何值,而不是“IN01”、“IN02”等,也可以是“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"
                
              ]
        
    ]




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

【问题讨论】:

你尝试过什么约会?不要只提供问题陈述并期待解决方案。展示你尝试过的东西,解释你研究过的东西,并展示这项研究如何让你失败。 这里没有日期值。我认为您误解了值:“11101111101”,这只是字符串 阅读 MDN 或任何其他 JS 文档。你会发现很多循环的例子。 “你尝试过什么约会”是指你到目前为止尝试过什么。 这不是 StackOveflow 的工作方式。我们期待you to do research 在仅仅询问代码之前。请拨打tour 并阅读help center 以获取有关如何提出好问题的更多信息 【参考方案1】:

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

【讨论】:

我觉得有一个小的改进结果,我想使用“days”而不是“value”键 @SoorajJose 代码已编辑,看看【参考方案2】:

使用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>

【讨论】:

以上是关于如何在javascript中将嵌套对象转换为对象数组? [关闭]的主要内容,如果未能解决你的问题,请参考以下文章

在Javascript中将数组转换为嵌套的JSON?

如何在javascript中将字符串转换为文件对象?

如何在Javascript中将字符串转换为对象? [复制]

如何在 Rails 中将 Javascript 对象转换为 JSON?

在Javascript中将嵌套对象测试为未定义[重复]

如何在 Python 中将日期时间对象转换为自纪元(unix 时间)以来的毫秒数?