将嵌套对象中的对象向上提起

发布于 2025-01-20 15:33:32 字数 2496 浏览 0 评论 0原文

我有一个树对象。在其最深层面上,有_Metrics带有值的密钥,我想要的是将_Metrics提升到其父对象。 我正在努力解决此案,因为这是我第一次遇到这样的情况。

给定输入

const d = {
    "enterprise1": {
        "site1": {
            "area1": {
                "unit1": {
                    "__metrics": {
                        "state_class": "down",
                        "state": "other",
                        "reason": "UNDEFINED",
                        "product": "sovereign",
                        "count": 10
                    }
                },
                "unit2": {
                    "__metrics": {
                        "state_class": "run",
                        "state": "running",
                        "reason": "UNDEFINED",
                        "product": "cola",
                        "count": 10
                    }
                }
            }
        }
    }
};

上面的输入将始终在第四级中具有量表对象,即“ unit”对象。

所需的输出

const d = {
    "enterprise1": {
      "__metrics": {
        "state_class": ["run", "down"],
        "state": ["running", "other"],
        "reason": "UNDEFINED",
        "product": ["cola", "sovereign"],
        "count": 20
      },
        "site1": {
          "__metrics": {
            "state_class": ["run", "down"],
            "state": ["running", "other"],
            "reason": "UNDEFINED",
            "product": ["cola", "sovereign"],
            "count": 20
          },
            "area1": {
               "__metrics": {
                  "state_class": ["run", "down"],
                  "state": ["running", "other"],
                  "reason": "UNDEFINED",
                  "product": ["cola", "sovereign"],
                  "count": 20
                },
                "unit1": {
                    "__metrics": {
                        "state_class": "down",
                        "state": "other",
                        "reason": "UNDEFINED",
                        "product": "sovereign",
                        "count": 10
                    }
                },
                "unit2": {
                    "__metrics": {
                        "state_class": "run",
                        "state": "running",
                        "reason": "UNDEFINED",
                        "product": "cola",
                        "count": 10
                    }
                }
            }
        }
    }
};

I have a tree object. In its deepest level there is _metrics key with value and what I want is to lift _metrics up to its parent object.
I am struggling to solve this case as it is the first time I encounter such a case.

Given Input

const d = {
    "enterprise1": {
        "site1": {
            "area1": {
                "unit1": {
                    "__metrics": {
                        "state_class": "down",
                        "state": "other",
                        "reason": "UNDEFINED",
                        "product": "sovereign",
                        "count": 10
                    }
                },
                "unit2": {
                    "__metrics": {
                        "state_class": "run",
                        "state": "running",
                        "reason": "UNDEFINED",
                        "product": "cola",
                        "count": 10
                    }
                }
            }
        }
    }
};

The input above will always have __metrics object in 4th level which is "unit" object.

Desired output

const d = {
    "enterprise1": {
      "__metrics": {
        "state_class": ["run", "down"],
        "state": ["running", "other"],
        "reason": "UNDEFINED",
        "product": ["cola", "sovereign"],
        "count": 20
      },
        "site1": {
          "__metrics": {
            "state_class": ["run", "down"],
            "state": ["running", "other"],
            "reason": "UNDEFINED",
            "product": ["cola", "sovereign"],
            "count": 20
          },
            "area1": {
               "__metrics": {
                  "state_class": ["run", "down"],
                  "state": ["running", "other"],
                  "reason": "UNDEFINED",
                  "product": ["cola", "sovereign"],
                  "count": 20
                },
                "unit1": {
                    "__metrics": {
                        "state_class": "down",
                        "state": "other",
                        "reason": "UNDEFINED",
                        "product": "sovereign",
                        "count": 10
                    }
                },
                "unit2": {
                    "__metrics": {
                        "state_class": "run",
                        "state": "running",
                        "reason": "UNDEFINED",
                        "product": "cola",
                        "count": 10
                    }
                }
            }
        }
    }
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深巷少女 2025-01-27 15:33:32

我猜 javascript 没有一个库来提升对象的键值。您可以使用 for-in 循环在每个级别中添加 __metrics 。 __metrics 的添加取决于对象的深度。

循环将是这样的:

for (let key_lvl1 in d) {
      //enterprise level
      d[key_lvl1]["__metrics"] = __metrics_both;
      d[key_lvl1] = Object.keys(d[key_lvl1])
        .sort()
        .reduce((obj, key) => {
          obj[key] = d[key_lvl1][key];
          return obj;
        }, {});
      //console.log(d[key_lvl1]);
      for (let key_lvl2 in d[key_lvl1]) {
        //site level
        if (key_lvl2 === "__metrics") continue;
        d[key_lvl1][key_lvl2]["__metrics"] = __metrics_both;   
        d[key_lvl1][key_lvl2] = Object.keys(d[key_lvl1][key_lvl2])
          .sort()
          .reduce((obj, key) => {
            obj[key] = d[key_lvl1][key_lvl2][key];
            return obj;
          }, {});
        //console.log(d[key_lvl1][key_lvl2]);
        for (let key_lvl3 in d[key_lvl1][key_lvl2]) {
          //area level
          if (key_lvl3 === "__metrics") continue;
          d[key_lvl1][key_lvl2][key_lvl3]["__metrics"] = __metrics_both;
          d[key_lvl1][key_lvl2][key_lvl3] = Object.keys(
            d[key_lvl1][key_lvl2][key_lvl3]
          )
            .sort()
            .reduce((obj, key) => {
              obj[key] = d[key_lvl1][key_lvl2][key_lvl3][key];
              return obj;
            }, {});
          //console.log(d[key_lvl1][key_lvl2][key_lvl3]);
        }
      }
    }

代码的整体可能是这样的:

const d = {
  enterprise1: {
    site1: {
      area1: {
        unit1: {
          __metrics: {
            state_class: "down",
            state: "other",
            reason: "UNDEFINED",
            product: "sovereign",
            count: 10
          }
        },
        unit2: {
          __metrics: {
            state_class: "run",
            state: "running",
            reason: "UNDEFINED",
            product: "cola",
            count: 10
          }
        }
      },
      area2: {
        unit3: {
          __metrics: {
            state_class: "down",
            state: "other",
            reason: "UNDEFINED",
            product: "sovereign",
            count: 10
          }
        },
        unit4: {
          __metrics: {
            state_class: "run",
            state: "running",
            reason: "UNDEFINED",
            product: "cola",
            count: 10
          }
        }
      }
    }
  }
};

let __metrics_both = {
  state_class: ["run", "down"],
  state: ["running", "other"],
  reason: "UNDEFINED",
  product: ["cola", "sovereign"],
  count: 20
};

for (let key_lvl1 in d) {
  //enterprise level
  d[key_lvl1]["__metrics"] = __metrics_both;
  d[key_lvl1] = Object.keys(d[key_lvl1])
    .sort()
    .reduce((obj, key) => {
      obj[key] = d[key_lvl1][key];
      return obj;
    }, {});
  //console.log(d[key_lvl1]);
  for (let key_lvl2 in d[key_lvl1]) {
    //site level
    if (key_lvl2 === "__metrics") continue;
    d[key_lvl1][key_lvl2]["__metrics"] = __metrics_both;   
    d[key_lvl1][key_lvl2] = Object.keys(d[key_lvl1][key_lvl2])
      .sort()
      .reduce((obj, key) => {
        obj[key] = d[key_lvl1][key_lvl2][key];
        return obj;
      }, {});
    //console.log(d[key_lvl1][key_lvl2]);
    for (let key_lvl3 in d[key_lvl1][key_lvl2]) {
      //area level
      if (key_lvl3 === "__metrics") continue;
      d[key_lvl1][key_lvl2][key_lvl3]["__metrics"] = __metrics_both;
      d[key_lvl1][key_lvl2][key_lvl3] = Object.keys(
        d[key_lvl1][key_lvl2][key_lvl3]
      )
        .sort()
        .reduce((obj, key) => {
          obj[key] = d[key_lvl1][key_lvl2][key_lvl3][key];
          return obj;
        }, {});
      //console.log(d[key_lvl1][key_lvl2][key_lvl3]);
    }
  }
}
console.log(d);

也许这不是最佳实践,但这几乎是您所期望的。如果在将数据对象添加到表中时在每个级别都添加 __metrics 会更好。希望你喜欢它:)

I guess javascript doesn't have a library to lift it up the key-value of an object. You could be use to add __metrics in each level using for-in loop. The add of __metrics is depend how many level deep the object do you have.

The looping would be like this:

for (let key_lvl1 in d) {
      //enterprise level
      d[key_lvl1]["__metrics"] = __metrics_both;
      d[key_lvl1] = Object.keys(d[key_lvl1])
        .sort()
        .reduce((obj, key) => {
          obj[key] = d[key_lvl1][key];
          return obj;
        }, {});
      //console.log(d[key_lvl1]);
      for (let key_lvl2 in d[key_lvl1]) {
        //site level
        if (key_lvl2 === "__metrics") continue;
        d[key_lvl1][key_lvl2]["__metrics"] = __metrics_both;   
        d[key_lvl1][key_lvl2] = Object.keys(d[key_lvl1][key_lvl2])
          .sort()
          .reduce((obj, key) => {
            obj[key] = d[key_lvl1][key_lvl2][key];
            return obj;
          }, {});
        //console.log(d[key_lvl1][key_lvl2]);
        for (let key_lvl3 in d[key_lvl1][key_lvl2]) {
          //area level
          if (key_lvl3 === "__metrics") continue;
          d[key_lvl1][key_lvl2][key_lvl3]["__metrics"] = __metrics_both;
          d[key_lvl1][key_lvl2][key_lvl3] = Object.keys(
            d[key_lvl1][key_lvl2][key_lvl3]
          )
            .sort()
            .reduce((obj, key) => {
              obj[key] = d[key_lvl1][key_lvl2][key_lvl3][key];
              return obj;
            }, {});
          //console.log(d[key_lvl1][key_lvl2][key_lvl3]);
        }
      }
    }

And the overall of the code could be like this:

const d = {
  enterprise1: {
    site1: {
      area1: {
        unit1: {
          __metrics: {
            state_class: "down",
            state: "other",
            reason: "UNDEFINED",
            product: "sovereign",
            count: 10
          }
        },
        unit2: {
          __metrics: {
            state_class: "run",
            state: "running",
            reason: "UNDEFINED",
            product: "cola",
            count: 10
          }
        }
      },
      area2: {
        unit3: {
          __metrics: {
            state_class: "down",
            state: "other",
            reason: "UNDEFINED",
            product: "sovereign",
            count: 10
          }
        },
        unit4: {
          __metrics: {
            state_class: "run",
            state: "running",
            reason: "UNDEFINED",
            product: "cola",
            count: 10
          }
        }
      }
    }
  }
};

let __metrics_both = {
  state_class: ["run", "down"],
  state: ["running", "other"],
  reason: "UNDEFINED",
  product: ["cola", "sovereign"],
  count: 20
};

for (let key_lvl1 in d) {
  //enterprise level
  d[key_lvl1]["__metrics"] = __metrics_both;
  d[key_lvl1] = Object.keys(d[key_lvl1])
    .sort()
    .reduce((obj, key) => {
      obj[key] = d[key_lvl1][key];
      return obj;
    }, {});
  //console.log(d[key_lvl1]);
  for (let key_lvl2 in d[key_lvl1]) {
    //site level
    if (key_lvl2 === "__metrics") continue;
    d[key_lvl1][key_lvl2]["__metrics"] = __metrics_both;   
    d[key_lvl1][key_lvl2] = Object.keys(d[key_lvl1][key_lvl2])
      .sort()
      .reduce((obj, key) => {
        obj[key] = d[key_lvl1][key_lvl2][key];
        return obj;
      }, {});
    //console.log(d[key_lvl1][key_lvl2]);
    for (let key_lvl3 in d[key_lvl1][key_lvl2]) {
      //area level
      if (key_lvl3 === "__metrics") continue;
      d[key_lvl1][key_lvl2][key_lvl3]["__metrics"] = __metrics_both;
      d[key_lvl1][key_lvl2][key_lvl3] = Object.keys(
        d[key_lvl1][key_lvl2][key_lvl3]
      )
        .sort()
        .reduce((obj, key) => {
          obj[key] = d[key_lvl1][key_lvl2][key_lvl3][key];
          return obj;
        }, {});
      //console.log(d[key_lvl1][key_lvl2][key_lvl3]);
    }
  }
}
console.log(d);

maybe it's not the best practice, but it's almost what you'd expect. it would be better if __metrics were added at each level when the data objects were added to the table. Hope you like it :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文