The flat method allows us to take a nested array and flatten in it by creating a new array with the values from the flattened arrays in it. flat also allows us to pass a numerical value for the depth of how far recursively will the flat method go in terms of lib values of the nested arrays into the new array.

Examples

const array1 = [1, 2, 3, 4, [5, 6]];
console.log(array1.flat()); //[1, 2, 3, 4, 5, 6]
const array2 = [7, 8, 9, 10, [[11], [12, 13]]];
console.log(array1.flat(3)); // [7, 8, 9, 10, 11, 12, 13]

In the last example above we use 3 as the depth because there are 3 arrays that need to be flattened so that the values that are in them can be taken and placed into the new array. We have one array with the value of 11, another array with the values of 12, 13 and finally an outer array that has those two arrays contained in it. If we did not specify a depth for the last example above, we would get the following result: [7, 8, 9, 10, [11], [12, 13]]. We could also use the value Infinity to get the same flat array result as in the last example: [7, 8, 9, 10, 11, 12, 13].