色婷婷狠狠18禁久久YY,CHINESE性内射高清国产,国产女人18毛片水真多1,国产AV在线观看

JavaScript如何獲取數(shù)組維度

吉茹定2年前28瀏覽0評論

JavaScript如何獲取數(shù)組維度?

function getDepth(arr) {

const eleDepths = []

arr.forEach( ele => {

let depth = 0

if (Array.isArray(ele)) {

depth = getDepth(ele)

}

eleDepths.push(depth)

})

return 1 + max(eleDepths)

}

function max(arr) {

return arr.reduce( (accu, curr) => {

if (curr > accu) return curr

return accu

})

}

// test

const arr1 = [1, 2, 3]

const arr2 = [1, 2, 3, [1, 2]]

const arr3 = [1, [1, [1, 2]], 3]

const arr4 = [1, [1, [1, 2]], 3, 4, [1, [1, [1, 3]]]]

console.log(getDepth(arr1)) // 1

console.log(getDepth(arr2)) // 2

console.log(getDepth(arr3)) // 3

console.log(getDepth(arr4)) // 4

php數(shù)組傳遞到html,JavaScript如何獲取數(shù)組維度