多维数组变1维

const arr = [1, 2, [2, 3, [3, 5]]];
方法1:
function arrOne(arr) {
return arr.reduce(function(total, currentVal) {
return total.concat(Array.isArray(currentVal) ? arrOne(currentVal) : currentVal)
}, [])
}
console.log(arrOne(arr))
方法2:
arr.flat(999)

获取DOM最大深度

function getDepth(dom){
if(!dom.children||dom.children.length==0){
return 1
}
const nextDepths = [...dom.children].map(item=>{
return getDepth(item)
})
return 1 + Math.max(...nextDepths)
}

获取数组维度 [1, 2, 3, [4], [[6], [5]]].getLevel()

Array.prototype.getLevel = function() {
if(this.length==0){
return 1
}
let nextLevel = this.map(item=>{
return Array.isArray(item)?item.getLevel():0
})
return 1+Math.max(...nextLevel)
};
Array.prototype.getLevel = function() {
let arr = this;
let j = 1
for (let item of arr) {
if (Array.isArray(item)) {
if (1 + item.getLevel() > j) {
j = j + item.getLevel()
}
}
}
return j
};
Array.prototype.getLevel = function() {
let arr = this
let arrJson = JSON.stringify(arr)
let nowArr = arrJson.replace(/[0-9]/g, "").replace(/,/g, "")
let i = 0;
return removeO(nowArr)

function removeO(arr) {
if (arr.indexOf('[]') != -1) {
i++
removeO(arr.replace(/\[\]/g, ""))
}
return i
}

}

[1, 1, ‘1’, ‘1’, null, null, undefined, undefined, new String(‘1’), new String(‘1’), /a/, /a/, NaN, NaN]

返回:

[1, ‘1’, null, undefined, new String(‘1’), /a/, NaN]

let arr = [1, 1, '1', '1', null, null, undefined, undefined, new String('1'), new String('1'), /a/, /a/, NaN, NaN, ]

let map = new Map()
for (let i = 0; i < arr.length; i++) {

if (typeof arr[i] == 'object') {
if (arr[i] instanceof RegExp) {
map.set(arr[i].toString(), arr[i])
} else {
map.set(JSON.stringify(arr[i]), arr[i])
}
} else {
map.set(arr[i], arr[i])
}
}
for (let [key, value] of map) {
console.log(value)
}