Array.flat() method in JavaScript

--

Today we gonna discuss the flat() method in JavaScript which can be a part of the interview in the way to becoming a JavaScript developer.

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

Syntax And Return Type 

flat(depth) :—  The depth level specifies how deep a nested array structure should be flattened. Default value is 1.
Return Type — A new array with sub-array elements concatenated into itExamples

let arr1 = [1,2,[3,4,[5,6,]]];

arr1.flat(1) // [1,2,3,4,[5,6]];

arr1.flat(2) // [1,2,3,4,5,6];

arr1.flat(0) // [1,2,[3,4,[5,6]]];

Hope this article may helpful to all !!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Rakesh Kumar Shaw
Rakesh Kumar Shaw

No responses yet

Write a response