// 比如这个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;}