TypeScript Array flat() Method Last Updated : 19 Jul, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report The Array.prototype.flat() method in TypeScript allows you to create a new array with all sub-array elements concatenated into it, up to a specified depth. This method helps in flattening nested arrays to the desired depth, making it easier to handle deeply nested structures.Syntax:array.flat([depth]); Parameters:depth (optional): The depth level specifies how deep a nested array structure should be flattened. It Defaults to 1.Return Value: Flat() method returns a new array with its sub-array elements that are concatenated into it.Examples Array flat() MethodExample 1: Flattening a Single-Level Nested ArrayIn this example, we will flatten a single-level nested array using the flat() method. JavaScript const singleLevelArray: number[][] = [ [1, 2], [3, 4], [5, 6] ]; const flatArray: number[] = singleLevelArray.flat(); console.log(flatArray); Output:[1, 2, 3, 4, 5, 6] Example 2: Flattening a Multi-Level Nested ArrayIn this example, we will flatten a multi-level nested array using the flat() method with a specified depth. JavaScript const multiLevelArray: any[] = [1, [2, [3, [4, [5, 6]]]]]; const flatArrayDepth2: any[] = multiLevelArray.flat(2); console.log(flatArrayDepth2); Output: [1, 2, 3, [4, [5, 6]]] The Array.prototype.flat() method is used for flattening nested arrays in TypeScript. It provides flexibility with the depth parameter, allowing you to control the level of flattening according to your needs. This method is especially useful when dealing with deeply nested array structures. Comment More infoAdvertise with us Next Article TypeScript Array flat() Method P pankajbind Follow Improve Article Tags : JavaScript Web Technologies TypeScript Similar Reads TypeScript Arrays An array is a user-defined data type. An array is a homogeneous collection of similar types of elements that have a contiguous memory location and which can store multiple values of different data types.An array is a type of data structure that stores the elements of a similar data type and consider 5 min read How to Flatten Array of Arrays in TypeScript ? Array flattening is the process of converting a nested array (an array of arrays) into a single-dimensional array where all the elements are at the same level. In other words, it involves transforming an array that contains other arrays as elements into an array that only contains non-array elements 4 min read JavaScript Array flat() Method The Javascript arr.flat() method was introduced in ES2019. The flat() method in JavaScript creates a new array with all sub-array elements concatenated into it recursively up to the specified depth. If no depth is provided, it defaults to 1.Syntax:arr.flat([depth])Parameters:This method accepts a si 4 min read JavaScript Array flatMap() Method The flatMap() method transforms each element of an array using a mapping function and flattens the result into a new array. It applies the function to every element, avoiding empty ones, and preserves the original array. Syntax:let A = array.flatMap(function callback(current_value, index, Array)) {/ 3 min read How to deep flatten an array in JavaScript? In this article, we will learn how to deep flatten an array in JavaScript. The flattening of an array is a process of merging a group of nested arrays present inside a given array. Deep flattening means that the array would be completely flattened. Example: Input: [1,2,3,4,5,[6,[7,8,9]]] Output: [1, 2 min read Like