javascript - JS如何获取多维数组的最大深度?

标签 javascript arrays object

我有一个多维数组,我想知道它的最大深度。

我找到了这个灵魂,但它不适用于对象数组:

const getArrayDepth = (arr) => {
  return Array.isArray(arr) ? 1 + Math.max(...arr.map(getArrayDepth)) : 0;
}
const a = [1, [[2, 3, [[4], 5], 6, [7, 8]], 9, [10, [[[11]]]], 12, 13], 14];

const b = [{
  "name": "Car Parts",
  "children": [{
    "name": "Body Parts",
    "children": [{
      "name": "Bumpers & Components",
      "children": [{
        "name": "Bumper Grilles",
        "children": []
      }]
    }]
  }, {
    "name": "Engine Parts",
    "children": [{
      "name": "Alternators",
      "children": []
    }, {
      "name": "Injectors",
      "children": []
    }]
  }]
}, {
  "name": "Wheels",
  "children": [{
    "name": "Alloy Wheels",
    "children": []
  }]
}, {
  "name": "Lubricants & Coolants",
  "children": []
}];

console.log(`A: ${getArrayDepth(a)} ✓`);
console.log(`B: ${getArrayDepth(b)} X (should be 4) `);

如何转换此代码以适用于我的数组?

最佳答案

参数arr要么是一个数组,要么不是。如果不是,则它要么是带有 children 数组(具有元素)的对象,要么不是。如果不是,则返回 0。否则,对 children 进行递归。

const getArrayDepth = (arr) => {
  return Array.isArray(arr) ? // if arr is an array, recurse over it
    1 + Math.max(...arr.map(getArrayDepth)) : 
    Array.isArray(arr.children) && arr.children.length ? // if arr is an object with a children property, recurse over the children
      1 + Math.max(...arr.children.map(getArrayDepth)) : 
      0; // otherwise, it's 0
}
const a = [1, [[2, 3, [[4], 5], 6, [7, 8]], 9, [10, [[[11]]]], 12, 13], 14];

const b = [{
  "name": "Car Parts",
  "children": [{
    "name": "Body Parts",
    "children": [{
      "name": "Bumpers & Components",
      "children": [{
        "name": "Bumper Grilles",
        "children": []
      }]
    }]
  }, {
    "name": "Engine Parts",
    "children": [{
      "name": "Alternators",
      "children": []
    }, {
      "name": "Injectors",
      "children": []
    }]
  }]
}, {
  "name": "Wheels",
  "children": [{
    "name": "Alloy Wheels",
    "children": []
  }]
}, {
  "name": "Lubricants & Coolants",
  "children": []
}];

console.log(`A: ${getArrayDepth(a)} ✓`);
console.log(`B: ${getArrayDepth(b)} X (should be 4) `);

这又是,使用 if 语句而不是嵌套三元组。我认为这样更清楚一些,但这实际上只是一个意见。

const getArrayDepth = (arr) => {
  if (Array.isArray(arr)) {
    // if arr is an array, recurse over it
    return 1 + Math.max(...arr.map(getArrayDepth));
  }
  if (arr.children && arr.children.length) {
    // if arr is an object with a children property, recurse over the children
    return 1 + Math.max(...arr.children.map(getArrayDepth));
  }
  return 0;
}
const a = [1, [[2, 3, [[4], 5], 6, [7, 8]], 9, [10, [[[11]]]], 12, 13], 14];

const b = [{
  "name": "Car Parts",
  "children": [{
    "name": "Body Parts",
    "children": [{
      "name": "Bumpers & Components",
      "children": [{
        "name": "Bumper Grilles",
        "children": []
      }]
    }]
  }, {
    "name": "Engine Parts",
    "children": [{
      "name": "Alternators",
      "children": []
    }, {
      "name": "Injectors",
      "children": []
    }]
  }]
}, {
  "name": "Wheels",
  "children": [{
    "name": "Alloy Wheels",
    "children": []
  }]
}, {
  "name": "Lubricants & Coolants",
  "children": []
}];

console.log(`A: ${getArrayDepth(a)} ✓`);
console.log(`B: ${getArrayDepth(b)} X (should be 4) `);

关于javascript - JS如何获取多维数组的最大深度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59162497/

相关文章:

javascript - 如何循环嵌套数组?

javascript - 如果数组返回子级或子级映射时如何正确替换任何类型

javascript - Sails.js 中的多个关联

javascript - 始终保持两个 div 之间的垂直距离相同

javascript - 有没有办法用javascript以相反的顺序在数组上使用map()?

java - 如何检查字符串数组的连续性&如果不连续,则将其删除?

java - java中如何将数组作为参数?

javascript - Ajax POST 使用 PHP 保存 JSON 本地文件

java - 复制数组——初始大小重要吗?

php - 为什么带有 mysqli 的面向对象的 PHP 比过程方法更好?