var arr1 = [1,[2,3,[5,4,[4,[1]]]]]
var arr2 = [1,2,2,2,2,4]
function getDeepth(array) {
function sum(arr, flag) {
return arr.reduce(function (total, item) {
var totalDeepth ;
if (Array.isArray(item)) {
totalDeepth = sum(item, flag + 1);
}
return totalDeepth > total ? totalDeepth : total;
}, flag)
}
return sum(array, 1);
}
function test(arr){
let result = Array.from(new Set( arr.flat(getDeepth(arr))))
document.write(result+"<br>")
}
test(arr1)
test(arr2)
结果:
1,2,3,5,4
1,2,4