递归地将数组展平到指定的深度并返回一个新数组。
Array.prototype.flat()
Syntax(语法): Array.prototype.flat(depth)
depth(深度) 默认值为一,可以无限延展至待碾平的数组的最深度
实例
const numbers = [1, 2, [3, 4, [5, 6]]];
// Considers default depth of 1
numbers.flat();
> [1, 2, 3, 4, [5, 6]]
// With depth of 2
numbers.flat(2);
> [1, 2, 3, 4, 5, 6]
// Executes two flat operations
numbers.flat().flat();
> [1, 2, 3, 4, 5, 6]
// Flattens recursively until the array contains no nested arrays
numbers.flat(Infinity)
> [1, 2, 3, 4, 5, 6]
网友评论