GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.geeksforgeeks.org — JavaScript Array flat() Method - GeeksforGeeks JavaScript Array flat() MethodLast Updated : 15 Jul, 2025 The Javascript arr.flat() method was introduced in ES2019. The Syntax:arr.flat([depth])Parameters:This method accepts a single parameter as mentioned above and described below:
Return value:It returns an array i.e. depth levels flatter than the original array, it removes nesting according to the depth levels. Example 1: Flattening a Multilevel Array The code initializes a multilevel array, then applies the flat() method with Infinity parameter to recursively flatten all nested arrays into a single-level array. The result is logged.
Output [ '1', '2', '3', '4', '5', '6', '7' ] Example 2: Flattening Nested Arrays with flat() Method The code demonstrates flattening a nested array to different levels using the flat() method. It applies flat() with various depth parameters (0, 1, 2) to flatten nested arrays accordingly and logs the results.
Output Zero levels flattened array: 1,2,3,,4,5,6 One level flattened array: 1,2,3,,4,5,6 Two level flattened array: 1,2,3,4,5,6 Three levels flattened array: 1,2,3,4,5,6 Note: For depth greater than 2, the array remains the same, since it is already flattened completely. Example 3: Flattening an Array with undefined Element This code creates an array
Output [ 1, 2, 3, 4 ] We have a complete list of Javascript Array methods, to check those please go through this Javascript Array Complete reference article. Supported Browsers:Comment Explore |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |