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 How to Build Multi Type Multidimensional Arrays in TypeScript ? P pankajbind Follow Improve Article Tags : 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 How to Build Multi Type Multidimensional Arrays in TypeScript ? TypeScript, multi-type multidimensional Arrays allow us to create arrays that can hold elements of multiple types at each level of the array hierarchy. we will learn how to build multi-type multidimensional arrays. These are the following methods: Table of Content Using Union TypesUsing Tuple TypesU 3 min read Ruby | Array class flatten() function flatten() is an Array class method which returns flattened array i.e. a 1D array Syntax: Array.flatten() Parameter: Array Return: 1D array Example #1 : Ruby # Ruby code for flatten() method # declaring array a = [[18, 22], [ 33, nil, 5, 6]] # declaring array b = [[[1, 4, 1, 1, 88, 9]]] # flatten put 1 min read Ruby | Array class flatten!() function flatten!() is a Array class method which returns the flattened the array and returns nil if there is no modification required Syntax: Array.flatten!() Parameter: Array Return: flattened array or nil if no modification is required Example #1 : Ruby # Ruby code for flatten!() method # declaring array 1 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 Like