// 比如这个a中,就有四层。如何算出这四层const a = { ?b: 1, ?c() {}, ?d: { ???e: 2, ???f: { ?????g: 3, ?????h: { ???????i: 4, ?????}, ???}, ???j: { ?????k: 5, ???}, ?},};
js
function testLevel(param) { ?const isObject = typeof param === ‘object‘ && !(param instanceof Array); ?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;}
js练习- 给你一个对象,求有几层
原文地址:https://www.cnblogs.com/qiqi715/p/10419697.html