// 比如这个a中,就有四层。如何算出这四层
const a = {
  b: 1,
  c() {},
  d: {
    e: 2,
    f: {
      g: 3,
      h: {
        i: 4,
      },
    },
    j: {
      k: 5,
    },
  },
};

js

function testLevel(param) {
  const isObject = Object.prototype.toString.call(param) === '[object Object]';
  if (!isObject) return 0;

  const level = 1;
  let childrenLevel = 0;
  Object.entries(param)
    .map(([key, value]) => {
      const valueLevel = testLevel(value, level);

      if (valueLevel > childrenLevel) childrenLevel = valueLevel;
    });
  return level + childrenLevel;
}

相关文章:

  • 2021-06-20
  • 2021-07-30
  • 2021-10-28
  • 2021-09-20
  • 2022-02-09
  • 2021-06-28
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-05-16
  • 2022-03-09
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案