js数组降维浅解
简单的二维数组降为
arr=['a',['b'],['c'],['d'],'e','f']//二维数组
arr.flatMap(v=>v)//flatMap()方法对原数组的每个成员执行一个函数(相当于执行Array.prototype.map())
//["a", "b", "c", "d", "e", "f"]
多维数组彻底降维
const flattenDeep = (arr) => Array.isArray(arr)
? arr.reduce( (a, b) => [...a, ...flattenDeep(b)] , [])
: [arr]
flattenDeep([1, [[2], [3, [4]], 5]])
//[1, 2, 3, 4, 5]