Skip to content

📚 10.getLeval

💻 代码实现

typescript
Array.prototype.getLeval = function () {
    let depth = 1
    const dfs = (arr) => {
        for (let i = 0; i < arr.length; i++) {
            if (Array.isArray(arr[i])) {
                depth++
                dfs(arr[i])
            }
        }
    }
    dfs(this)   
    return depth
}

let arr = [1, 2, 3, [4, [5]]]
console.log(arr.getLeval())

Released under the MIT License.