# flat拍平数组

const arr = [
  1,
  2,
  3,
  4,
  [1, 2, 3, [1, 2, 3, [1, 2, 3]]],
  5,
  "string",
  { name: "弹铁蛋同学" },
];

function flat(arr) {
  return arr.reduce(
    (totalArr, item) =>
      totalArr.concat(Array.isArray(item) ? flat(item) : item),
    []
  );
}

console.log(flat(arr));
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Last Updated: 6/27/2023, 7:40:45 PM