【问题标题】:Reformatting deeply nested JSON object with varying length into an array of simplified objects将不同长度的深度嵌套 JSON 对象重新格式化为简化对象数组
【发布时间】:2019-08-22 14:20:33
【问题描述】:

here 所述,我正在使用 map-reduce 函数将多个数据输入合并到一个对象中。

收到的reduced Object格式如下:

{
    "2019-04-02T00:00:00-04:00": {
        "2019-04-02T09:00:00-04:00": {
            "2019-04-02T18:00:00-04:00": {
                "[MET] L2 - NB": {
                    "attendees": [
                        "Lex Luthor",
                        "Lois Lane"
                    ]
                },
                "[MET] L2 -  CS": {
                    "attendees": [
                        "General Zod",
                        "Clark Kent"
                    ]
                }
            }
        }
    },
    "2019-04-03T00:00:00-04:00": {
        "2019-04-03T09:00:00-04:00": {
            "2019-04-03T18:00:00-04:00": {
                "[MET] L2 - NB": {
                    "attendees": [
                        "Lex Luthor",
                        "Lois Lane"
                    ]
                },
                "[MET] L2 -  CS": {
                    "attendees": [
                        "General Zod",
                        "Clark Kent"
                    ]
                }
            }
        }
    }
}

但是,我正在寻找一种方法将其重新格式化为对象数组,以便遍历对象并轻松访问所有数据:

[
    {
        Date: "2019-04-02T00:00:00-04:00",
        StartTimeLocalized: "2019-04-02T09:00:00-04:00",
        EndTimeLocalized: "2019-04-02T18:00:00-04:00",
        LabelWithCompany: "[MET] L2 - NB",
        attendees: [
            "Lex Luthor",
            "Lois Lane"
        ]
    }, {
        Date: "2019-04-02T00:00:00-04:00",
        StartTimeLocalized: "2019-04-02T09:00:00-04:00",
        EndTimeLocalized: "2019-04-02T18:00:00-04:00",
        LabelWithCompany: "[MET] L2 -  CS",
        attendees: [
            "General Zod",
            "Clark Kent"
        ]
    },
    {
        Date: "2019-04-03T00:00:00-04:00",
        StartTimeLocalized: "2019-04-03T09:00:00-04:00",
        EndTimeLocalized: "2019-04-03T18:00:00-04:00",
        LabelWithCompany: "[MET] L2 -  CS",
        attendees: [
            "Lex Luthor",
            "Lois Lane"
        ]
    },
    {
        Date: "2019-04-03T00:00:00-04:00",
        StartTimeLocalized: "2019-04-03T09:00:00-04:00",
        EndTimeLocalized: "2019-04-03T18:00:00-04:00",
        LabelWithCompany: "[MET] L2 -  CS",
        attendees: [
            "General Zod",
            "Clark Kent"
        ]
    }
]

【问题讨论】:

    标签: javascript node.js javascript-objects


    【解决方案1】:

    您可以通过传递嵌套键数组并获取新对象的所有键来采取动态方法。

    function mapNestedObjects(object, keys) {
        function getNested(source, target, index) {
            Object.entries(source).forEach(([key, value]) => {
                if (index + 1 < keys.length) {
                    getNested(value, { ...target, [keys[index]]: key }, index + 1);
                } else {
                    result.push({ ...target, [keys[index]]: key, ...value });
                }
            });
        }
    
        var result = [];
        getNested(object, {}, 0);
        return result;
    }
    
    var object = { "2019-04-02T00:00:00-04:00": { "2019-04-02T09:00:00-04:00": { "2019-04-02T18:00:00-04:00": { "[MET] L2 - NB": { attendees: ["Lex Luthor", "Lois Lane"] }, "[MET] L2 -  CS": { attendees: ["General Zod", "Clark Kent"] } } } }, "2019-04-03T00:00:00-04:00": { "2019-04-03T09:00:00-04:00": { "2019-04-03T18:00:00-04:00": { "[MET] L2 - NB": { attendees: ["Lex Luthor", "Lois Lane"] }, "[MET] L2 -  CS": { attendees: ["General Zod", "Clark Kent"] } } } } },
        keys = ["Date", "StartTimeLocalized", "EndTimeLocalized", "LabelWithCompany"],
        result = mapNestedObjects(object, keys);
    
    console.log(result);
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    【讨论】:

      【解决方案2】:

      const obj = {
          "2019-04-02T00:00:00-04:00": {
              "2019-04-02T09:00:00-04:00": {
                  "2019-04-02T18:00:00-04:00": {
                      "[MET] L2 - NB": {
                          "attendees": [
                              "Lex Luthor",
                              "Lois Lane"
                          ]
                      },
                      "[MET] L2 - CS": {
                          "attendees": [
                              "General Zod",
                              "Clark Kent"
                          ]
                      }
                  }
              }
          },
          "2019-04-03T00:00:00-04:00": {
              "2019-04-03T09:00:00-04:00": {
                  "2019-04-03T18:00:00-04:00": {
                      "[MET] L2 - NB": {
                          "attendees": [
                              "Lex Luthor",
                              "Lois Lane"
                          ]
                      },
                      "[MET] L2 - CS": {
                          "attendees": [
                              "General Zod",
                              "Clark Kent"
                          ]
                      }
                  }
              }
          }
      };
      
      const getObjecta = (object, val) => {
        const a = {}, b = {};
        a.Date = val;
        b.Date = val;
      
        a.StartTimeLocalized = Object.keys(object)[0];
        b.StartTimeLocalized = Object.keys(object)[0];
      
        a.EndTimeLocalized = Object.keys(object[a.StartTimeLocalized])[0];
        b.EndTimeLocalized = Object.keys(object[b.StartTimeLocalized])[0];
      
        a.LabelWithCompany = '[MET] L2 - NB';
        b.LabelWithCompany = '[MET] L2 - CS';
      
        a.attendees = object[a.StartTimeLocalized][a.EndTimeLocalized]['[MET] L2 - NB']['attendees'];
        b.attendees = object[b.StartTimeLocalized][b.EndTimeLocalized]['[MET] L2 - CS']['attendees'];
      
        return [a, b];
      };
      
      let finalArray = [];
      
      Object.keys(obj).forEach((val) => {
      	finalArray = [...finalArray, ...getObjecta(obj[val], val)];
      });
      
      console.log(finalArray);

      【讨论】:

        【解决方案3】:

        您可以有 4 级嵌套循环并将结果推送到数组中。要映射对象,您可以使用Object.entries 将每个级别的键值对作为数组获取

        var obj = {
            "2019-04-02T00:00:00-04:00": {
                "2019-04-02T09:00:00-04:00": {
                    "2019-04-02T18:00:00-04:00": {
                        "[MET] L2 - NB": {
                            "attendees": [
                                "Lex Luthor",
                                "Lois Lane"
                            ]
                        },
                        "[MET] L2 -  CS": {
                            "attendees": [
                                "General Zod",
                                "Clark Kent"
                            ]
                        }
                    }
                }
            },
            "2019-04-03T00:00:00-04:00": {
                "2019-04-03T09:00:00-04:00": {
                    "2019-04-03T18:00:00-04:00": {
                        "[MET] L2 - NB": {
                            "attendees": [
                                "Lex Luthor",
                                "Lois Lane"
                            ]
                        },
                        "[MET] L2 -  CS": {
                            "attendees": [
                                "General Zod",
                                "Clark Kent"
                            ]
                        }
                    }
                }
            }
        }
        
        const res = [];
        Object.entries(obj).forEach(([date, value]) => {
        
          Object.entries(value).forEach(([start, value2]) => {
            Object.entries(value2).forEach(([end, value3]) => {
                Object.entries(value3).forEach(([label, value4]) => {
                  res.push({
                    Date: date,
                    StartTimeLocalized: start,
                    EndTimeLocalized: end,
                    LabelWithCompany: label,
                    attendees: value4.attendees
                  })
                })
            })
          })
        })
        
        console.log(res)

        【讨论】:

          猜你喜欢
          • 2019-09-04
          • 1970-01-01
          • 2020-06-21
          • 1970-01-01
          • 2020-12-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2023-01-13
          相关资源
          最近更新 更多