Array.flat() in JavaScript

Hello Devs,

In this article i will walk you through one of the important methods of Array in JavaScript i.e Array.flat method.

Table of content

Theory
Syntax
Return Value
Examples

So without wasting any time lets get into the article


This content originally appeared on DEV Community and was authored by capscode

Hello Devs,

In this article i will walk you through one of the important methods of Array in JavaScript i.e Array.flat method.

Table of content

  1. Theory
  2. Syntax
  3. Return Value
  4. Examples

So without wasting any time lets get into the article

Theory

The flat() method is used to flatten the array of arrays. The process of flatten depends on the depth we want to flattened the array of arrays.

What this method do is, it takes out the array element from an array of arrays and concat them in the main array.
it will repeat this process till the depth value.

Syntax

Syntax:
flat(depth)
// The depth level specifying how deep a nested array structure should be flattened.
// Defaults to 1.

Return Value

return value:
A new array with the sub-array elements concatenated into it.

Examples

example:
let arr = [1, 2, [3, 4, [5, 6]]];

arr.flat(0) // [1, 2, [3, 4, [5, 6]]];
arr.flat(1) // [1, 2, 3, 4, [5, 6]]
arr.flat(2) // [1, 2, 3, 4, 5, 6]
arr.flat(3) // [1, 2, 3, 4, 5, 6] 
arr1.flat( ) // [1, 2, 3, 4, [5, 6]]
// if no depth is provided then it will take the default value i.e 1.

arr.flat(0).length //3
arr.flat(1).length //5
arr.flat(2).length //6

Thank you for reading this far. This is a brief introduction of Array.flat() method in JavaScript .
If you find this article useful, like and share this article. Someone could find it useful too. If you find anything technically inaccurate please feel free to comment below.

Hope its a nice and informative read for you.
VISIT https://www.capscode.in/#/blog TO LEARN MORE...
See you in my next Blog article, Take care!!

Thanks,
@capscode


This content originally appeared on DEV Community and was authored by capscode


Print Share Comment Cite Upload Translate Updates
APA

capscode | Sciencx (2021-07-13T06:28:00+00:00) Array.flat() in JavaScript. Retrieved from https://www.scien.cx/2021/07/13/array-flat-in-javascript/

MLA
" » Array.flat() in JavaScript." capscode | Sciencx - Tuesday July 13, 2021, https://www.scien.cx/2021/07/13/array-flat-in-javascript/
HARVARD
capscode | Sciencx Tuesday July 13, 2021 » Array.flat() in JavaScript., viewed ,<https://www.scien.cx/2021/07/13/array-flat-in-javascript/>
VANCOUVER
capscode | Sciencx - » Array.flat() in JavaScript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/07/13/array-flat-in-javascript/
CHICAGO
" » Array.flat() in JavaScript." capscode | Sciencx - Accessed . https://www.scien.cx/2021/07/13/array-flat-in-javascript/
IEEE
" » Array.flat() in JavaScript." capscode | Sciencx [Online]. Available: https://www.scien.cx/2021/07/13/array-flat-in-javascript/. [Accessed: ]
rf:citation
» Array.flat() in JavaScript | capscode | Sciencx | https://www.scien.cx/2021/07/13/array-flat-in-javascript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.