Javascript实现平铺任意维度的嵌套数组
2018-11-02 本文已影响30人
Hi小胡
function flattenArray(arr) {
const flattened = [].concat(...arr);
return flattened.some(item => Array.isArray(item)) ?
flattenArray(flattened) : flattened;
}
const arr = [11, [22, 33],
[44, [55, 66, [77, [88]], 99]]
];
const flatArr = flattenArray(arr);
console.log(flatArr);//=> [11, 22, 33, 44, 55, 66, 77, 88, 99]