1 求数组的深度
function getArrayDepth(arr) {
// 判断是否为数组
if (!Array.isArray(arr)) {
return 0;
}
let depth = 0;
// 递归计算深度
arr.forEach(item => {
if (Array.isArray(item)) {
let currentDepth = getArrayDepth(item);
depth = Math.max(depth, currentDepth);
}
});
// 当前层级加1,因为当前层级也需要计算在内
return depth + 1;
}
// 示例
console.log(getArrayDepth([1, 2, [3, 4, [5, 6]]])); // 输出: 3
console.log(getArrayDepth([1, 2, 3])); // 输出: 1
console.log(getArrayDepth([])); // 输出: 1
2 微前端 数据隔离 实现