js Array flat
时间: 2023-10-03 18:03:20 浏览: 55
JavaScript Array.flat()函数用法解析
The `flat()` method is used to flatten nested arrays. It takes an optional argument, `depth`, which specifies how many levels of nesting to flatten. If no argument is provided, it defaults to 1.
Syntax:
```
array.flat([depth])
```
Example:
```
const arr = [1, 2, [3, 4], [5, [6, 7]]];
const flattenedArr = arr.flat(); // [1, 2, 3, 4, 5, [6, 7]]
const deeplyFlattenedArr = arr.flat(2); // [1, 2, 3, 4, 5, 6, 7]
```
阅读全文