# 1. 数组扁平化
# 1.1 描述
实现数组扁平化
# 1.2 实现
function flatten(list, depth = 1) {
if (depth === 0) return list;
return list.reduce(
(a, b) => a.concat(Array.isArray(b) ? flatten(b, depth - 1) : b),
[]
);
}
const a = flatten([1, 2, 3, [4, [5, 6]]]);
const b = flatten([1, 2, 3, [4, [5, 6]]], 2);
console.log(a, b);
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12